7

Default zero values for all Go types

 3 years ago
source link: https://yourbasic.org/golang/default-zero-value/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Default zero values for all Go types

yourbasic.org/golang
empty-hands.jpg

Variables declared without an initial value are set to their zero values:

  • 0 for all integer types,
  • 0.0 for floating point numbers,
  • false for booleans,
  • "" for strings,
  • nil for interfaces, slices, channels, maps, pointers and functions.

The elements of an array or struct will have its fields zeroed if no value is specified. This initialization is done recursively:

type T struct {
    n int
    f float64
    next *T
}
fmt.Println([2]T{}) // [{0 0 <nil>} {0 0 <nil>}]

Further reading

catalog-card-thumb.jpg

Struct initialization code example

Share:             


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK