A Booklet of notes about the Go language. INTRO Go was created by Google recently and has a reputation for creating fast executable code. It is a compiled language which is not really object oriented. * read a file line by line. --------- package main import ( "bufio" "fmt" "log" "os") func main() { file, err := os.Open("/path/to/file.txt") if err != nil { log.Fatal(err) } defer file.Close() scanner := bufio.NewScanner(file) // optionally, resize scanner's capacity for lines // over 64K, see next example for scanner.Scan() { fmt.Println(scanner.Text()) } if err := scanner.Err(); err != nil { log.Fatal(err) } } ,,, CSV There is a csv package for go. But you cant refer to fields by field names. Thats ok. SYNTAX Go has a c style syntax but ... * this is illegal ----- func blah { blah } ,,, Its hard to believe but you cant put the { on the next line. What does this mean? Why and wherefore? Does it mean that the creators of go don't know how to parse? surely not. I think one of the golang designers was also an original clang designer. This is truly bizarre in my opinion. My guess it that the compiler is somehow recursive-descent and it was simpler and faster to enforce the curly brace on the same line rule. Or that somehow the compiler used a "readline" function in its tokenizer .... Or that someone decided to become a curly brace fascist owing to some emotional disorder. COMPILING Is often referred to as 'building'. * compile single file program >> go build eg.go * compile some go file and name the output after the folder its in >> go build COMMAND LINE ARGS Also see the "flags" package which parses and provides access to flag on the cli, for example "somecom -f x.txt -l" The os.Args is a slice not an array if that means anything. * print the first command line argument ----- arg := os.Args[1] fmt.Println(arg) ,,,, * get the command line arguments ----- package main import ( "fmt" "os") func main() { argsWithProg := os.Args argsWithoutProg := os.Args[1:] arg := os.Args[3] fmt.Println(argsWithProg) fmt.Println(argsWithoutProg) fmt.Println(arg) } ,,,, STRINGS * trim whitespace >> strings.TrimSpace() * remove last char of string >> feet[:len(feet)-1] * initialize string with implicit type >> s := "hi" * concatenate and initialize >> s := "he" + "llo" * assign >> s = "hi" CONVERTING STRINGS TO SOMETHING ELSE * remove last char and convert to float >> feetFloat, _ := strconv.ParseFloat(feet[:len(feet)-1], 64) * convert string to integer >> i := strconv.Atoi(s) STDIN * get a value from stdin and convert to float ----- reader := bufio.NewReader(os.Stdin) fmt.Println("Enter feet value: \n") feet, _ := reader.ReadString('\n') feetFloat, _ := strconv.ParseFloat(feet[:len(feet)-1], 64) ,,,, PROCEDURES * procedure syntax, returns array of arrays of strings ----- readCsvFile(filePath string) [][]string { } ,,,, VARIABLES You have to use declared variables or else you get a compile error!!! But this is in keeping with go's "holier than thou" attitude. Not surprising. * error if you dont use 'i' !!! >> i := 1 * initialize and assign >> i := 2 * assign >> i = 3 TYPES AND STRUCTURES * a queue of structures (string int pair) ------ type job struct { url string depth int } queue := make(chan job) queue <- job{url, depth} ,,,, * a person struct with initializer ------- type person struct { name string age int } func newPerson(name string) *person { p := person{name: name} p.age = 42; return &p; } func main() { fmt.Println(person{"Bob", 20}) } ,,, * pointer to struct >> sp := &s METHODS * structure and method ------ type rect struct { width, height int } func (r *rect) area() int { return r.width * r.height } // call with r.area() ,,,,