VARIS VONGUEA-AREE

PHP 🐍 Python 🔥 Golang 🇹🇭 Bangkok Thailand

High level experience in web design and development knowledge, producing quality work.

For more info
  • Home
  • About
  • Project
  • Blog
  • Varisz
  • Golang
  • Golang Read CSV

Golang Read CSV

package main

import (
	"encoding/csv"
	"fmt"
	"os"
)
type Websites struct {
	ID          string
	Country     string
	Group       string
}


func main(){

	csvFile, err := os.Open("test.csv")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("Successfully Opened CSV file")
	defer csvFile.Close()

	csvLines, err := csv.NewReader(csvFile).ReadAll()
	if err != nil {
		fmt.Println(err)
	}

        var count float64
	for _, line := range csvLines {
		count += 1
		website := Websites{
			ID:          line[0],
			Country:     line[1],
			Group:       line[2],

		}

		fmt.Println(count, website.ID+" "+website.Country)

	}
}

© 2021 All rights reserved by varisz