Categories
csv

Read a CSV file

Comma-Separated Value (CSV) file, it’s likely that applications like Excel export spreadsheets into CSV file or a custom application program encode it data into CSV format for you. I assumed you getting in UTF-8 encoded.

Instead of hard coding the filenames into the source code, filename will pass into the progam as parameters (arguments)

Public Dataset

https://www.kaggle.com/datasets

Our example dataset (snip)

DATE_TIME,PLANT_ID,SOURCE_KEY,DC_POWER,AC_POWER,DAILY_YIELD,TOTAL_YIELD
15-05-2020 00:00,4135001,1BY6WEcLGh8j5v7,0,0,0,6259559
15-05-2020 00:00,4135001,1IF53ai7Xc0U56Y,0,0,0,6183645
15-05-2020 00:00,4135001,3PZuoBAID5Wc2HD,0,0,0,6987759

First row, the header names, and from the second row our dataset separated by a comma each field match the header names

package main
 
import (
    "encoding/csv"
    "fmt"
    "os"
)
 
func main() {
 
    file, err := os.Open(os.Args[1])
    if err != nil {
        fmt.Println(err)
    }
    reader := csv.NewReader(file)
    records, _ := reader.ReadAll()
    // Dump the list 
    fmt.Println(records)
}

Resources

https://golang.org/pkg/encoding/csv/

https://golang.org/pkg/encoding/csv/#pkg-examples

Categories
io/ioutil

Read a File with io/ioutil

package main

import (
	"fmt"
	"io/ioutil"
	"log"
)

// import os modules if filename pass as parameters

// ReadFile function open the file
func ReadFile() {

	fmt.Printf("\n\nReading a file with ioutil.ReadFile \n")
	// embedded filename into the source code but alternative it can be dynamic
	// by pass the filename as parameters/arguments during execution of program
	// using the OS modules and argv methods
	fileName := "sample.txt"
	// alternative solution here....
	// add import of os module or IDE automatically add for us
	// uncomment the line below and comment the above line.
	// fileNameViaArgv := os.Args[1]
	data, err := ioutil.ReadFile("sample.txt")
	// uncomment the line below and comment the above line using alternative solution
	// dataArgv, err := ioutil.ReadFile(fileNameViaArgv)
	if err != nil {
		log.Panicf("ioutil.ReadFile return nil when failed reading data from file: %s", err)
	}
	fmt.Printf("\nFile Name: %s", fileName)
	fmt.Printf("\nSize: %d bytes", len(data))
	fmt.Printf("\nData: %s", data)
	// uncomment this if you using OS modules as alternative solution
	/* fmt.Printf("\nFile Name: %s", fileNameViaArgv)
	fmt.Printf("\nSize: %d bytes", len(dataArgv))
	fmt.Printf("\nData: %s", dataArgv) */
}

func main() {
	ReadFile()
}

https://golang.org/pkg/io/ioutil/#example_ReadFile

// ReadFile reads the file named by filename and returns the contents.
// A successful call returns err == nil, not err == EOF. Because ReadFile
// reads the whole file, it does not treat an EOF from Read as an error
// to be reported.
func ReadFile(filename string) ([]byte, error) {
	f, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	defer f.Close()
	// It's a good but not certain bet that FileInfo will tell us exactly how much to
	// read, so let's try it but be prepared for the answer to be wrong.
	var n int64 = bytes.MinRead

	if fi, err := f.Stat(); err == nil {
		// As initial capacity for readAll, use Size + a little extra in case Size
		// is zero, and to avoid another allocation after Read has filled the
		// buffer. The readAll call will read into its allocated internal buffer
		// cheaply. If the size was wrong, we'll either waste some space off the end
		// or reallocate as needed, but in the overwhelmingly common case we'll get
		// it just right.
		if size := fi.Size() + bytes.MinRead; size > n {
			n = size
		}
	}
	return readAll(f, n)
}

Categories
regexp Regular Expression

Regular Expression

regrex package

https://golang.org/pkg/regexp/

On-line Regular Expression

https://regex101.com/

Meta Characters

Metacharacters are characters that are interpreted in a special way by a RegEx engine. Here’s a list of metacharacters:

[] . ^ $ * + ? {} () \ |

[] – Square brackets

Square brackets specifies a set of characters you wish to match.

. – Period

A period matches any single character (except newline '\n').

 – Caret

The caret symbol ^ is used to check if a string starts with a certain character.

$ – Dollar

The dollar symbol $ is used to check if a string ends with a certain character.

* – Star

The star symbol * matches zero or more occurrences of the pattern left to it.

+ – Plus

The plus symbol + matches one or more occurrences of the pattern left to it.

? – Question Mark

The question mark symbol ? matches zero or one occurrence of the pattern left to it.

{} – Braces

Consider this code: {n,m}. This means at least n, and at most m repetitions of the pattern left to it.

| – Alternation

Vertical bar | is used for alternation (or operator).

() – Group

Parentheses () is used to group sub-patterns.

\ – Backslash

Backlash \ is used to escape various characters including all metacharacters.

Special Sequences

Special sequences make commonly used patterns easier to write.

Categories
strings

https://golang.org/pkg/strings/

func Split

func Split(s, sep string) []string

Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators.

If s does not contain sep and sep is not empty, Split returns a slice of length 1 whose only element is s.

If sep is empty, Split splits after each UTF-8 sequence. If both s and sep are empty, Split returns an empty slice.

It is equivalent to SplitN with a count of -1.

Categories
crypto/sha256 Standard Library

SHA265 – cypto/sha256

Package sha256 implements the SHA224 and SHA256 hash algorithms as defined in FIPS 180-4.

https://golang.org/pkg/crypto/sha256/

Categories
My Go Tutorial os Standard Library

os package Reading from the opened file.

func (*File) Read

func (f *File) Read(b []byte) (n int, err error)
// Read reads up to len(b) bytes from the File.
// It returns the number of bytes read and any error encountered.
// At end of file, Read returns 0, io.EOF.
func (f *File) Read(b []byte) (n int, err error) {
	if err := f.checkValid("read"); err != nil {
		return 0, err
	}
	n, e := f.read(b)
	return n, f.wrapErr("read", e)
}

Read method, one take []byte slice argument. Return two values namely n int type and err error type.

In this example, an assumption that the file already opens with os.Open(). a make initializes the variable on a slice of []byte type with has a quantity of 100 of memory of bytes size (uint8 – unsigned 8-bit integers).

data := make([]byte, 100)
count, err := file.Read(data)
if err != nil {
	log.Fatal(err)
}
fmt.Printf("read %d bytes: %q\n", count, data[:count])

file.Read(data) read a stream of 100 bytes into the count variable contains the number in int type and err variable contains the error value

Note: file contents may have CR character, will be counted

Categories
My Go Tutorial os Standard Library

os package with reading an open file and error log

package os

import (
	"errors"
	"internal/poll"
	"internal/testlog"
	"io"
	"runtime"
	"syscall"
	"time"
)

the os package import a few other packages.

file, err := os.Open("file.go") // For read access.
if err != nil {
	log.Fatal(err)
}
// Open opens the named file for reading. If successful, methods on
// the returned file can be used for reading; the associated file
// descriptor has mode O_RDONLY.
// If there is an error, it will be of type *PathError.
func Open(name string) (*File, error) {
	return OpenFile(name, O_RDONLY, 0)
}

Open opens the named file for reading. If successful, methods on the returned file can be used for reading; the associated file descriptor has mode O_RDONLY. If there is an error, it will be of type *PathError.

type File
File represents an open file descriptor.

type File struct {
    // contains filtered or unexported fields
}
file, err := os.Open("file.go") // For read access.

file variable is the file descriptor which has mode O_RDONLY

Note: Next, read contents of pointer type by file variable name

Categories
bufio Standard Library

Reader

type Reader struct {
	buf          []byte
	rd           io.Reader // reader provided by the client
	r, w         int       // buf read and write positions
	err          error
	lastByte     int // last byte read for UnreadByte; -1 means invalid
	lastRuneSize int // size of last rune read for UnreadRune; -1 means invalid
}
// NewReader returns a new Reader whose buffer has the default size.
func NewReader(rd io.Reader) *Reader {
	return NewReaderSize(rd, defaultBufSize)
}
package main

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

func main() {
	scanner := bufio.NewReader(os.Stdin)
	fmt.Print("Enter text: ")
	text,_ := scanner.ReadString('\n')
	fmt.Printf("You entered: %s", text)
}
Categories
bufio Standard Library

Writer and Flush

package main

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

func main() {
	w := bufio.NewWriter(os.Stdout)
	fmt.Fprint(w, "Hello, ")
	fmt.Fprint(w, "world!")
	w.Flush() // Don't forget to flush!
}
Categories
bufio Standard Library

What is a buffer?

Send data to a buffer (system memory) and will not write until it reaches the length. After which it writes the data and flushes the buffer.