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
+7.4k Golang : Get today's weekday name and calculate target day distance example
+4.4k Unix/Linux : How to pipe/save output of a command to file?
+20.6k Golang : Get password from console input without echo or masked
+7.8k Golang : Routes multiplexer routing example with regular expression control
+28.8k Golang : JQuery AJAX post data to server and send data back to client example
+18.1k Golang : Set, Get and List environment variables
+4.7k Golang : Convert lines of string into list for delete and insert operation
+9.8k Golang : How to get quoted string into another string?
+5.7k Golang : Extract XML attribute data with attr field tag example
+18.2k Golang : Padding data for encryption and un-padding data for decryption
+8.8k Golang : Generate random Chinese, Japanese, Korean and other runes
+40.4k Golang : How to count duplicate items in slice/array?