What should be the values of GOPATH and GOROOT?

GOPATH is discussed in the cmd/go documentation: The GOPATH environment variable lists places to look for Go code. On Unix, the value is a colon-separated string. On Windows, the value is a semicolon-separated string. On Plan 9, the value is a list. GOPATH must be set to get, build and install packages outside the standard … Read more

What is the best way to test for an empty string in Go?

Both styles are used within the Go’s standard libraries. if len(s) > 0 { … } can be found in the strconv package: http://golang.org/src/pkg/strconv/atoi.go if s != “” { … } can be found in the encoding/json package: http://golang.org/src/pkg/encoding/json/encode.go Both are idiomatic and are clear enough. It is more a matter of personal taste and … Read more

Pointers vs. values in parameters and return values

tl;dr: Methods using receiver pointers are common; the rule of thumb for receivers is, “If in doubt, use a pointer.” Slices, maps, channels, strings, function values, and interface values are implemented with pointers internally, and a pointer to them is often redundant. Elsewhere, use pointers for big structs or structs you’ll have to change, and … Read more

When is the init() function run?

Yes assuming you have this: var WhatIsThe = AnswerToLife() func AnswerToLife() int { // 1 return 42 } func init() { // 2 WhatIsThe = 0 } func main() { // 3 if WhatIsThe == 0 { fmt.Println(“It’s all a lie.”) } } AnswerToLife() is guaranteed to run before init() is called, and init() is … Read more

Reading a file line by line in Go

In Go 1.1 and newer the most simple way to do this is with a bufio.Scanner. Here is a simple example that reads lines from a file: 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) … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)