Golang : Null and nil value
Programmers coming from different programming languages to Golang might wonder how does one deal with NULL values. I put down these reminders below about NULL value for myself and maybe this can be useful to you as well.
Here you go :
Golang does not support NULL.
Nil is the equivalent of NULL in Golang.
All variables by default are initiated with the nil(zero) value.
Example usage of nil :
correctHash := sha1.New() fmt.Printf("%x \n", correctHash.Sum(nil))
Function returning nil will NOT compile,
// not OK func getName() string { return nil }
but returning nil to a pointer or reference (such as a struct) is ok and will compile.
type Person struct { name string } // OK func getName() *Person { return nil }
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+4.9k Golang : Find change in a combination of coins example
+7.9k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+9.6k Golang : Command line file upload program to server example
+5.1k Golang : Extract XML attribute data with attr field tag example
+5.2k Linux/Unix : Commands that you need to be careful about
+5.4k Golang : Break string into a slice of characters example
+7.5k Golang : How to join strings?
+5.7k Get Facebook friends working in same company
+4.7k Javascript : How to refresh page with JQuery ?
+21.1k Golang : Read a file into an array or slice example
+4.7k Fix yum-complete-transaction error
+7.8k Golang : Write multiple lines or divide string into multiple lines