Skip to content

Instantly share code, notes, and snippets.

@stnc
Created February 5, 2026 11:51
Show Gist options
  • Select an option

  • Save stnc/dde9a78afa9251a116fe1837259b6600 to your computer and use it in GitHub Desktop.

Select an option

Save stnc/dde9a78afa9251a116fe1837259b6600 to your computer and use it in GitHub Desktop.
csv go
package main
import (
"encoding/csv"
"fmt"
"io"
"log"
"os"
"reflect"
// "strconv"
"strings"
)
type Person struct {
ID int `json:"id"`
Name string `json:"name"`
LastName string `json:"lastname"`
ZipCode string `json:"zipcode"`
City string `json:"city"`
Color string `json:"color"`
}
// var people []Person
// Müller, Hans, 67742 Lauterecken, 1
func main() {
//cvsAdded()
file, err := os.Open("data.csv")
if err != nil {
panic(err)
}
defer file.Close()
reader := csv.NewReader(file)
// if err != nil {
// panic(err)
// }
var people []Person
// DebugType(people)
// return
lineNum := 0
for {
record, err := reader.Read()
fmt.Println(record)
bosCheck(&people, record)
// fmt.Println(people)
if err == io.EOF {
break
}
if err != nil {
log.Printf("Satır %d okuma hatası: %v", lineNum+1, err)
continue
}
}
fmt.Printf("%+v\n", people)
}
func bosCheck(perons *[]Person, recorddata []string) {
// fmt.Println("recorddata")
// fmt.Println(recorddata)
// fmt.Println("------people-----")
// var perons *[]Person
var yeniVeri Person
for i := 0; i < len(recorddata); i++ {
fmt.Println(recorddata[i])
// id, _ := strconv.Atoi(recorddata[i])
// perons.Name = recorddata[0]
// perons.City = recorddata[1]
// perons.LastName = recorddata[2]
yeniVeri = Person{Name: recorddata[0], City: "berlin", LastName: recorddata[1], ZipCode: recorddata[2], Color: recorddata[3], ID: i}
// perons.City = "dd"
}
*perons = append(*perons, yeniVeri)
// fmt.Printf("%+v\n", perons)
// return &perons
}
func cvsAdded() {
file, err := os.Open("data.csv")
if err != nil {
log.Fatal(err)
}
defer file.Close()
reader := csv.NewReader(file)
// reader.FieldsPerRecord = -1 // Değişken kolon sayısına izin ver (lax mode) [web:23]
// Header oku
header, err := reader.Read()
if err != nil {
log.Fatal("Header okunamadı:", err)
}
expectedCols := len(header)
fmt.Printf("Header kolon sayısı: %d (ilk satır: %v)\n", expectedCols, header)
lineNum := -1
for {
record, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
log.Printf("Satır %d okuma hatası: %v", lineNum+1, err)
continue
}
lineNum++
colCount := len(record)
isEmpty := true
fmt.Println(record)
// DebugType(record)
fmt.Println(" ****************************top record**********")
for _, cell := range record {
if strings.TrimSpace(cell) != "" {
isEmpty = false
break
}
}
// bosCheck(record)
if colCount != expectedCols {
//fmt.Printf("Satır %d: Eksik kolon! Beklenen %d, var %d: %v\n", lineNum, expectedCols, colCount, record)
} else if isEmpty {
//fmt.Printf("Satır %d: Boş satır: %v\n", lineNum, record)
} else {
// fmt.Printf("Satır %d: Tamam (%d kolon): %v\n", lineNum, colCount, record)
fmt.Println("tamamdir")
// İşle: struct'e map et vs.
}
}
fmt.Println("Kontrol tamamlandı.")
}
func DebugType(v any) {
t := reflect.TypeOf(v)
fmt.Println("Type:", t.String())
fmt.Println("Kind:", t.Kind())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment