Golang >> How to Read Text,CSV,JSON,Console(stdin),YAML,environment variables in Golang

2022-05-09 Golang

Table of Contents

This tutorial shows how to read contents from text files, CSV, JSON, Console(stdin), environment variables, yaml file using golang.

Golang read file

Golang read file line by line to string

This example shows how to read a text file line by line to string.

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func main() {
	// Read the file
	text := readFile("test.txt")
	// Print the file
	fmt.Println(text)
}

// Define a function to read a text file line by line 
func readFile(filename string) string {
	// Open the file
	file, err := os.Open(filename)
	if err != nil {
		panic(err)
	}
	// Close the file when the function returns
	defer file.Close()
	// Read the file, line by line
	var lines []string
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		lines = append(lines, scanner.Text())
	}
	// Check for errors
	if err := scanner.Err(); err != nil {
		panic(err)
	}
	// Return the lines as a single string
	return strings.Join(lines, "\n")
}

Golang read CSV files

Read a CSV file and split each line by delimiter

We can read the CSV file as a normal plain text file and process it line by line.

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func main() {
	// Read the file
	text := readCSV("a.csv", ",")
	// Print the file
	fmt.Println(text)
}

// Define a function to read a CSV file and split each line by delimiter
func readCSV(filename string, delimiter string) []string {
	// Open the file
	file, err := os.Open(filename)
	if err != nil {
		panic(err)
	}
	// Close the file when the function returns
	defer file.Close()
	// Read the file, line by line
	var lines []string
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		lines = append(lines, strings.Split(scanner.Text(), delimiter)...)
	}
	// Check for errors
	if err := scanner.Err(); err != nil {
		panic(err)
	}
	// Return the lines as a single string
	return lines
}

Read a CSV file using encoding/csv

We can also use the encoding/csv package to read CSV files.

package main

import (
	"encoding/csv"
	"fmt"
	"io"
	"os"
)

func main() {
	// Read the file
	text := readCSV2("a.csv", ",")
	// Print the file
	fmt.Println(text)
}

// Use encoding/csv to read CSV files
func readCSV2(filename string, delimiter string) []string {

	// Open the file
	file, err := os.Open(filename)
	if err != nil {
		panic(err)
	}
	// Close the file when the function returns
	defer file.Close()

	// Read the file, line by line
	var lines []string
	reader := csv.NewReader(file)

	// Change the delimiter
	reader.Comma = []rune(delimiter)[0]

	for {
		record, err := reader.Read()
		if err == io.EOF {
			break
		}
		if err != nil {
			panic(err)
		}

		// Ignore the first line
		if len(record) > 0 {
			lines = append(lines, record...)
		}
	}
	// Return the lines as a single string
	return lines
}

Golang read JSON files

We can also read JSON files using the encoding/json package.

For example, we have a JSON file named students.json that has the following contents:

{
    "students": [
      {
        "name": "Kevin",
        "gender": "M",
        "age": 20,
        "scores": {
          "Math": 80,
          "Art": 78
        }
      },
      {
        "name": "Mary",
        "gender": "F",
        "age": 20,
        "scores": {
          "Math": 82,
          "Art": 75
        }
      }
    ]
  }

We can read the students.json file as follows.
Firstly, we define the Struct that we want to read.
And then we use the encoding/json package to read the file into the Struct.

package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
)

// Define a Students struct containing a Student array
type Students struct {
	Students []Student `json:"students"`
}

// Student struct definition
type Student struct {
	Name   string `json:"name"`
	Gender string `json:"gender"`
	Age    int    `json:"age"`
	Score  Score  `json:"scores"`
}

// Score struct which contains the scores of Math and Art
type Score struct {
	Math int `json:"Math"`
	Art  int `json:"Art"`
}

func main() {
	// Open the JSON file
	jsonFile, err := os.Open("students.json")
	// if an error occurs, print the error message
	if err != nil {
		fmt.Println(err)
	}

	defer jsonFile.Close()

	// Read the opened JSON file as a byte array.
	byteValue, _ := ioutil.ReadAll(jsonFile)

	var students Students

	// Unmarshal the content of JSON file(byteArray) into students
	json.Unmarshal(byteValue, &students)

	// loop through each student in the array
	// print out the Student Name, Gender, Age, Math score
	for i := 0; i < len(students.Students); i++ {
		fmt.Println("Student Name: " + students.Students[i].Name)
		fmt.Println("Student Gender: " + students.Students[i].Gender)
		fmt.Println("Student Age: " + strconv.Itoa(students.Students[i].Age))
		fmt.Println("Math Score: " + strconv.Itoa(students.Students[i].Score.Math))
	}
}

Golang read yaml file

We can use “gopkg.in/yaml.v2” package to read yaml files.

For example, we have a yaml file named config.yaml that has the following contents:

 database:
   host: localhost
   port: 3306
   user: root
   password: root
   database: test
 server:
   host: localhost
   port: 8080

We define a Configuration struct consisting of a DatabaseConfiguration and ServerConfiguration struct and then use the “gopkg.in/yaml.v2” package to read the file into the Struct.

// Read a YAML file to get the configuration

package main

import (
	"fmt"
	"log"
	"os"

	"gopkg.in/yaml.v2"
)

func main() {
	// Read the configuration file
	configuration := readYAML("config.yaml")
	configuration.Print()
}

func readYAML(file string) (configuration Configuration) {
	// Read the configuration file
	configurationFile, err := os.Open(file)
	if err != nil {
		log.Fatal(err)
	}
	defer configurationFile.Close()

	// Parse the configuration file
	yaml.NewDecoder(configurationFile).Decode(&configuration)
	return
}

// Configuration is the configuration of the application
type Configuration struct {
	Database DatabaseConfiguration
	Server   ServerConfiguration
}

// DatabaseConfiguration is the configuration of the database
type DatabaseConfiguration struct {
	Host     string
	Port     int
	User     string
	Password string
	Database string
}

// ServerConfiguration is the configuration of the server
type ServerConfiguration struct {
	Host string
	Port int
}

// Print the configuration
func (c Configuration) Print() {
	fmt.Println("Database:")
	fmt.Println("Host:", c.Database.Host)
	fmt.Println("Port:", c.Database.Port)
	fmt.Println("User:", c.Database.User)
	fmt.Println("Password:", c.Database.Password)
	fmt.Println("Database:", c.Database.Database)
	fmt.Println("Server:")
	fmt.Println("Host:", c.Server.Host)
	fmt.Println("Port:", c.Server.Port)
}

Golang read file

Golang read content from Console (Stdin)

We can use the “bufio” package to read content from Console (Stdin).

package main

import (
	"bufio"
	"fmt"
	"os"
)

// Read input from console
func main() {
	reader := bufio.NewReader(os.Stdin)
	fmt.Print("Enter your name: ")
	text, _ := reader.ReadString('\n')
	fmt.Println("Hello", text)
}
go run .\main.go
Enter your name: Kevin
Hello Kevin

Golang read environment variables

We can use the “os” package to read environment variables.

package main

import (
	"fmt"
	"os"
)

// Read environment variables from system
func main() {
	fmt.Println("My name is", os.Getenv("NAME"))
	fmt.Println("My age is", os.Getenv("AGE"))
}
export NAME=Kevin
export AGE=20
$ go run main.go   
My name is Kevin  
My age is 20  

Subscribe and be the FIRST reader of our latest articles

* indicates required

Contact us