Yup! Chosen by the person writing the program.
Not at all. Zero value is just the default value for any type when you do not specify a value on initialisation.
Hi Cherolyn,
It’s documented in the Go Programming Language Specification here:
https://golang.org/ref/spec#The_zero_value
When you declare a variable and don’t initialize it (or when creating variables other ways), Go initializes the variable to a default, called the zero value. You can think of it sort of like a zero, blank, or “nothing” value. In the case of numbers, zero is used. For bool
s, false is used, and for strings, the empty string (""
) is used.
This allows Go to be more concise, and also guarantees that all data is initialized to something. So declaring variables as
var i int = 0
var s string = ""
var b bool = false
does the same thing as
var i int
var s string
var b bool
I looked up a definition for default value and I don’t understand it. Can you help me with this?
I know type means classification of data, and I recall that definition whenever I see the term. It is very helpful to me.
I know value and I am slowly getting initialization.
I’m reading over this. Very interesting, and thanks.
Here is a helpful definition of default: a preselected option adopted by a computer program or other mechanism when no alternative is specified by the user or programmer.
And the information that follows is very helpful. I will save this reply to a file.
Thanks everyone.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.