How do you print in a Go test using the “testing” package?

The structs testing.T and testing.B both have a .Log and .Logf method that sound to be what you are looking for. .Log and .Logf are similar to fmt.Print and fmt.Printf respectively. See more details here: http://golang.org/pkg/testing/#pkg-index fmt.X print statements do work inside tests, but you will find their output is probably not on screen where … Read more

Declare a constant array

An array isn’t immutable by nature; you can’t make it constant. The nearest you can get is: var letter_goodness = […]float32 {.0817, .0149, .0278, .0425, .1270, .0223, .0202, .0609, .0697, .0015, .0077, .0402, .0241, .0675, .0751, .0193, .0009, .0599, .0633, .0906, .0276, .0098, .0236, .0015, .0197, .0007 } Note the […] instead of []: it … Read more

Can functions be passed as parameters?

Yes, consider some of these examples: package main import “fmt” // convert types take an int and return a string value. type convert func(int) string // value implements convert, returning x as string. func value(x int) string { return fmt.Sprintf(“%v”, x) } // quote123 passes 123 to convert func and returns quoted string. func quote123(fn … Read more

Parsing RFC-3339 / ISO-8601 date-time string in Go

Use the exact layout numbers described here and a nice blogpost here. so: layout := “2006-01-02T15:04:05.000Z” str := “2014-11-12T11:45:26.371Z” t, err := time.Parse(layout, str) if err != nil { fmt.Println(err) } fmt.Println(t) gives: >> 2014-11-12 11:45:26.371 +0000 UTC I know. Mind boggling. Also caught me first time. Go just doesn’t use an abstract syntax for … Read more

Is it OK to leave a channel open?

It’s OK to leave a Go channel open forever and never close it. When the channel is no longer used, it will be garbage collected. Note that it is only necessary to close a channel if the receiver is looking for a close. Closing the channel is a control signal on the channel indicating that … Read more

How to set default values in Go structs

One possible idea is to write separate constructor function //Something is the structure we work with type Something struct { Text string DefaultText string } // NewSomething create new instance of Something func NewSomething(text string) Something { something := Something{} something.Text = text something.DefaultText = “default text” return something }

How does Go update third-party packages?

go get will install the package in the first directory listed at GOPATH (an environment variable which might contain a colon separated list of directories). You can use go get -u to update existing packages. You can also use go get -u all to update all packages in your GOPATH For larger projects, it might … Read more

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