nil is a predeclared identifier in golang. It is the literal representation of zero values or say uninitialized value of many kinds of types. Many of us programmers who are new to golang, and are coming from a different language, may find it a bit difficult to digest.

Like in ruby, the below code is perfectly normal:

a = 'abcd'
a = '1234' if a == nil

However something similar in go will throw error:

func SomeFunction(name string) string {
	if name == nil { something here

The reason being, in Go, nil can represent zero values of the following kinds of types:

  1. pointer types (including type-unsafe ones)

2. map types

3. slice types

4. function types

5. channel types

6.interface types.

However if you do want a check whether a string is "" or empty, you can always try the below code:

func SomeFunction(name string) string {
	if len(name) == 0 { something here

Consider the following variable declarations:

var a *SomeType
var b interface{}
var c func()

In the above sample, it seems natural that all these variables would have a value that represents uninitialized state. a has been declared as a pointer, but what would it point to, when we haven't yet pointed it at anything? nil is an obvious zero value for these types.