type Ptr struct {
ID *big.Int
IpAddress string
Port string
}
var NewVar Ptr
After initializing the NewVar with values I then want to set NewVar to nil. How can I do this?
nil is not an allowed value for a struct. It is however, a common value for a pointer.
To actually be able to assign a new value ( nil ) to the pointer variable, we have to dereference it ( * operator).
About nil. nil is a predefined identifier in Go that represents zero values of many types. nil is usually mistaken as null (or NULL) in other languages, but they are different. Note: nil can be used without declaring it.
In the Go programming language, nil is a zero value. Recall from unit 2 that an integer declared without a value will default to 0. An empty string is the zero value for strings, and so on. A pointer with nowhere to point has the value nil .
The zero value of a struct value is not nil
Each element of such a variable or value is set to the zero value for its type:
false
for booleans,0
for integers,0.0
for floats,""
for strings, andnil
for pointers, functions, interfaces, slices, channels, and maps.
In your case, this variable declaration var NewVar Ptr
creates the variable, binds corresponding identifier Ptr
to it, and gives it a type and an initial value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With