You can easily check if a key exists in a go map by using the ok result: value, ok := myMap[key] Or in an one line check: if val, ok := myMap[key]; ok { // do something }
You can easily check if a key exists in a go map by using the ok result: value, ok := myMap[key] Or in an one line check: if val, ok := myMap[key]; ok { // do something }
If you work with weeknummers, you can find the weeknumber of a date with the Golang function time.ISOWeek. However you wish to get the first date of a week, there is no such function in Google Go. So let's write it our selfs: func FirstDayOfISOWeek(year int, week int, timezone *time.Location) time.Time { date := time.Date(year, 0, […]
Sometimes you want to know how long it takes to execute a piece of code. This is how to do it Golang: func main() { start := time.Now() // Do something time consuming elapsed := time.Since(start) log.Printf("Time to execute code %s", elapsed) }
Creating a directory from Google Go is simple with the "os" package. See the example below. Golang create directory example path := "/folder/to/create" err := os.MkdirAll(path,0711) if err != nil { log.Println("Error creating directory") log.Println(err) return }
When you use range in a Golang html/template the "." variable changes to the current object from the range. We can however still access to old "." variable with "$": {{range $index, $emp := .employees}} {{ $emp.name }} {{ msg $ "btn_show" }} {{ end }}
Strings are slices in Google Go, so there is really no need for a Golang substr function. For example if you would want to get the first 10 characters of a string: dt := "2015-01-01 15:45:12" d := dt[0:10] The resulting value in d will be: "2015-01-01".