Go is a popular and trendy programming language. But Go developers often face particular common bugs and errors. Developers address them as Gotchas. Golang is a comparatively new programming language by Google. Native gophers often face these pitfalls, so here we came with some common mistakes and its solutions. Checkout the following Gotchas and if you have already come across them in Go programming journey, know the solutions of it.
Top 10 Common Mistakes In Go Programming-
1. Multiple-value in single-value context-
Issue-
t := time.Parse(time.RFC3339, “2018-04-06T10:49:05Z”)
fmt.Println(t)
../main.go:9:17: multiple-value time.Parse() in single-value context
When you try to parse the date and time, you get a compiler error.
Solution-
t, err := time.Parse.RFC3339, “2018-04-06T10:49:05Z”)
if err != nil {
// TODO: Handle error.
}
fmt.Println(t)
2018-04-06 10:49:05 +0000 UTC
The parse function with time returns a time value and error value, and explicitly you need to use them. Or To ignore the unwanted error values, you can use a blank identifier _as below:
m := map[string]float64{“pi”: 3.1416}
_, exists := m[“pi”] // exists == true
Know more at- https://solaceinfotech.com/blog/top-10-common-mistakes-in-go-programming/
Top comments (0)