Getting started
Basic types
Variables
Constants
Strings
Pointers
Arrays
Slices
Maps
Structs
Interfaces
Empty interface
if, switch, goto
for, while loops
range statement
Functions
Methods
Error handling
Defer
Panic and recover
Concurrency
Channels and select
Mutex
Packages
Files and I/O
Time and date
Command line arguments
Logging
Executing commands
Hex, base64 encoding
JSON
XML
CSV
YAML
SQL
HTTP Client
HTTP Server
Text and HTML templates
Reflection
Context
Package fmt
OS Signals
Testing
Calling C from GO with cgo
Profiling using go tool pprof
Cross compilation
Conditional compilation with build tags
Inlining functions
sync.Pool for better performance
gob
Plugin
HTTP server middleware
Protobuf in Go
Console I/O
Cryptography
Images (PNG, JPEG, BMP, TIFF, WEBP, VP8, GIF)
The Go Command
Testing code with CI services
Windows GUI programming
Contributors

Zero values

suggest change

Variables in Go are initialized with a known value if not explicitly assigned.

That value is knows as zero value.

This is different from C/C++, where variables that are not explicitly assigned have undefined values.

The values of zero type are unsurprising:

type zero value
bool false
integers 0
floating point numbers 0.0
string ""
pointer nil
slice nil
map nil
interface nil
channel nil
array all elements have zero value
struct all members set to zero value
function nil

Said differently:

 fmt.Println("zero values for basic types:")

var zeroBool bool
fmt.Printf("bool:       %v\n", zeroBool)

var zeroInt int
fmt.Printf("int:        %v\n", zeroInt)

var zeroF32 float32
fmt.Printf("float32:    %v\n", zeroF32)

var zeroF64 float64
fmt.Printf("float64:    %v\n", zeroF64)

var zeroStr string
fmt.Printf("string:     %#v\n", zeroStr)

var zeroPtr *int
fmt.Printf("pointer:    %v\n", zeroPtr)

var zeroSlice []uint32
fmt.Printf("slice:      %v\n", zeroSlice)

var zeroMap map[string]int
fmt.Printf("map:        %#v\n", zeroMap)

var zeroInterface interface{}
fmt.Printf("interface:  %v\n", zeroInterface)

var zeroChan chan bool
fmt.Printf("channel:    %v\n", zeroChan)

var zeroArray [5]int
fmt.Printf("array:      %v\n", zeroArray)

type struc struct {
	a int
	b string
}
var zeroStruct struc
fmt.Printf("struct:     %#v\n", zeroStruct)

var zeroFunc func(bool)
fmt.Printf("function:   %v\n", zeroFunc)
zero values for basic types:
bool:       false
int:        0
float32:    0
float64:    0
string:     ""
pointer:    <nil>
slice:      []
map:        map[string]int(nil)
interface:  <nil>
channel:    <nil>
array:      [0 0 0 0 0]
struct:     main.struc{a:0, b:""}
function:   <nil>

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents