package main import ( "encoding/csv" "fmt" "strings" ) func main() { input := `name,age,city John,25,New York Jane,30,Los Angeles` reader := csv.NewReader(strings.NewReader(input)) reader.Read() // skip the first line record, err := reader.Read() if err != nil { fmt.Println("Error:", err) return } fmt.Println(record) // ["John", "25", "New York"] }
package main import ( "encoding/csv" "fmt" "os" ) func main() { file, err := os.Open("file.csv") if err != nil { fmt.Println("Error:", err) return } defer file.Close() reader := csv.NewReader(file) for { record, err := reader.Read() if err != nil { fmt.Println("Error:", err) break } fmt.Println(record) } }In this example, we are reading from a csv file "file.csv" using the os package. We create a csv reader from the opened file and then read each line (record) using a for loop until we reach the end of the file. We print each record to the console. Package library: encoding/csv