{"id":1080,"date":"2020-01-04T15:06:55","date_gmt":"2020-01-04T15:06:55","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=1080"},"modified":"2020-01-04T17:24:41","modified_gmt":"2020-01-04T17:24:41","slug":"go-default-zero-value-all-types","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/go-default-zero-value-all-types\/","title":{"rendered":"Default Zero Value of all Types in Go (Golang) With Examples"},"content":{"rendered":"\n

Default Value Table<\/strong><\/p>\n\n\n\n

Type<\/strong><\/td>Default Value<\/strong><\/td><\/tr>
Integer<\/td>0<\/td><\/tr>
Float<\/td>0<\/td><\/tr>
Complex Number<\/td>0 Real and 0 Imaginary Part<\/td><\/tr>
Byte<\/td>0<\/td><\/tr>
Rune<\/td>0<\/td><\/tr>
String<\/td>“”<\/td><\/tr>
Bool<\/td>false<\/td><\/tr>
Array<\/td>Every array value to its default<\/td><\/tr>
Struct<\/td>Every field to its default<\/td><\/tr>
Map<\/td>nil<\/td><\/tr>
Channel<\/td>nil<\/td><\/tr>
Interface<\/td>nil<\/td><\/tr>
Slices<\/td>nil<\/td><\/tr>
Pointer<\/td>nil<\/td><\/tr>
Function<\/td>nil<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n

<\/p>\n\n\n\n

Examples:<\/strong><\/h1>\n\n\n\n

Let’s see an example of each. Use the menu on the side to navigate.<\/p>\n\n\n\n

Integer<\/strong><\/h2>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n    var a int\n    fmt.Print(\"Default zero value of int: \")\n    fmt.Println(a)\n    \n    var b uint\n    fmt.Print(\"Default zero value of uint: \")\n    fmt.Println(b)\n}<\/code><\/pre>\n\n\n\n

Output:<\/strong><\/p>\n\n\n\n

Default zero value of int: 0\nDefault zero value of uint: 0<\/code><\/pre>\n\n\n\n

<\/p>\n\n\n\n

Float<\/strong><\/h2>\n\n\n\n
package main\nimport \"fmt\"\nfunc main() {\n    var a float32\n    fmt.Print(\"Default zero value of float: \")\n    fmt.Println(a)\n}<\/code><\/pre>\n\n\n\n

Output:<\/strong><\/p>\n\n\n\n

Default zero value of float: 0<\/code><\/pre>\n\n\n\n

<\/p>\n\n\n\n

Complex Number<\/strong><\/h2>\n\n\n\n
package main\nimport \"fmt\"\nfunc main() {\n    var a complex64\n    fmt.Print(\"Default zero value of complex: \")\n    fmt.Println(a)\n}<\/code><\/pre>\n\n\n\n

Output:<\/strong><\/p>\n\n\n\n

Default zero value of complex: (0+0i)<\/code><\/pre>\n\n\n\n

<\/p>\n\n\n\n

Byte<\/strong><\/h2>\n\n\n\n
package main\nimport \"fmt\"\nfunc main() {\n    var a byte\n    fmt.Print(\"Default zero value of byte: \")\n    fmt.Println(a)\n}<\/code><\/pre>\n\n\n\n

Output:<\/strong><\/p>\n\n\n\n

Default zero value of byte: 0<\/code><\/pre>\n\n\n\n

<\/p>\n\n\n\n

Rune<\/strong><\/h2>\n\n\n\n
package main\nimport \"fmt\"\nfunc main() {\n    var a rune\n    fmt.Print(\"Default zero value of rune: \")\n    fmt.Println(a)\n}<\/code><\/pre>\n\n\n\n

Output:<\/strong><\/p>\n\n\n\n

Default zero value of rune: 0<\/code><\/pre>\n\n\n\n

<\/p>\n\n\n\n

String<\/strong><\/h2>\n\n\n\n
package main\nimport \"fmt\"\nfunc main() {\n    var a string\n    fmt.Print(\"Default zero value of string: \")\n    fmt.Println(a)\n}<\/code><\/pre>\n\n\n\n

Output:<\/strong><\/p>\n\n\n\n

Default zero value of string:    \/\/An emtpy string. Hence nothing is printed<\/code><\/pre>\n\n\n\n

<\/p>\n\n\n\n

Bool<\/strong><\/h2>\n\n\n\n
package main\nimport \"fmt\"\nfunc main() {\n    var a bool\n    fmt.Print(\"Default zero value of bool: \")\n    fmt.Println(a)\n}<\/code><\/pre>\n\n\n\n

Output:<\/strong><\/p>\n\n\n\n

Default zero value of bool: false<\/code><\/pre>\n\n\n\n

<\/p>\n\n\n\n

Array<\/strong><\/h2>\n\n\n\n

Default value of an array is the default value of its values. For eg in the below code, there is an array of two lengths of type bool. When we print the output is [false false]<\/strong><\/p>\n\n\n\n

package main\nimport \"fmt\"\nfunc main() {\n    var a [2]bool\n    fmt.Print(\"Default Zero Value of an array: \")\n    fmt.Println(a)\n}<\/code><\/pre>\n\n\n\n

Output:<\/strong><\/p>\n\n\n\n

Default Zero Value of a array: [false false]<\/code><\/pre>\n\n\n\n

<\/p>\n\n\n\n

Struct<\/strong><\/h2>\n\n\n\n

Default value of a struct<\/strong> is the default value of its field. For eg in the below code, there is a struct sample with two field. One is of int type and the other is of the bool type. We create an instance of this struct and when we print it, the output is {0 false} <\/strong><\/p>\n\n\n\n

package main\nimport \"fmt\"\nfunc main() {\n    type sample struct {\n        a int\n        b bool\n    }\n    a := sample{}\n    fmt.Print(\"Default Zero Value of an struct: \")\n    fmt.Println(a)\n}<\/code><\/pre>\n\n\n\n

Output:<\/strong><\/p>\n\n\n\n

Default Zero Value of a struct: {0 false} <\/code><\/pre>\n\n\n\n

<\/p>\n\n\n\n

Map<\/strong><\/h2>\n\n\n\n

Default value of a map<\/strong> is nil. <\/strong>That is why the output of fmt.Println(a==nil)<\/strong> is true. When a map is passed to fmt.Println <\/strong>, it tries to print values in the map. That is why the output is map[]<\/p>\n\n\n\n

package main\nimport \"fmt\"\nfunc main() {\n    var a map[bool]bool\n    fmt.Println(a == nil)\n    fmt.Print(\"Printing map: \")\n    fmt.Println(a)\n}<\/code><\/pre>\n\n\n\n

Output:<\/strong><\/p>\n\n\n\n

true\nPrinting map: map[]<\/code><\/pre>\n\n\n\n

<\/p>\n\n\n\n

Channel<\/strong><\/h2>\n\n\n\n
package main\nimport \"fmt\"\nfunc main() {\n    var a chan int\n    fmt.Print(\"Default zero value of channel: \")\n    fmt.Println(a)\n}<\/code><\/pre>\n\n\n\n

Output:<\/strong><\/p>\n\n\n\n

Default zero value of channel: <nil><\/p>\n\n\n\n

<\/p>\n\n\n\n

Interface<\/strong><\/h2>\n\n\n\n
package main\nimport \"fmt\"\nfunc main() {\n    var a chan interface{}\n    fmt.Print(\"Default zero value of interface: \")\n    fmt.Println(a)\n}<\/code><\/pre>\n\n\n\n

Output:<\/strong><\/p>\n\n\n\n

Default zero value of interface: < nil><\/p>\n\n\n\n

<\/p>\n\n\n\n

Slice<\/strong><\/h2>\n\n\n\n

Default value of a slice is nil. <\/strong>That is why the output of fmt.Println(a==nil)<\/strong> is true. When a slice is passed to fmt.Println <\/strong>, it tries to print values in the slice. That is why the output is []<\/p>\n\n\n\n

package main\nimport \"fmt\"\nfunc main() {\n    var a []int\n    fmt.Println(a == nil)\n    fmt.Print(\"Printing slice: \")\n    fmt.Println(a)\n}<\/code><\/pre>\n\n\n\n

Output:<\/strong><\/p>\n\n\n\n

true\nPrinting slice: []<\/code><\/pre>\n\n\n\n

<\/p>\n\n\n\n

Function<\/strong><\/h2>\n\n\n\n
package main\nimport \"fmt\"\nfunc main() {\n    var a func()\n    fmt.Print(\"Default Zero Value of a func: \")\n    fmt.Println(a)\n}<\/code><\/pre>\n\n\n\n

Output:<\/strong><\/p>\n\n\n\n

Default Zero Value of a func: <nil><\/p>\n\n\n\n

<\/p>\n\n\n\n

Pointer<\/strong><\/h2>\n\n\n\n
package main\nimport \"fmt\"\nfunc main() {\n    var a *int\n    fmt.Print(\"Default Zero Value of a pointer: \")\n    fmt.Println(a)\n}<\/code><\/pre>\n\n\n\n

Output:<\/strong><\/p>\n\n\n\n

Default Zero Value of a pointer: <nil><\/p>\n","protected":false},"excerpt":{"rendered":"

Default Value Table Type Default Value Integer 0 Float 0 Complex Number 0 Real and 0 Imaginary Part Byte 0 Rune 0 String “” Bool false Array Every array value to its…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[143,140,142,3,4,117,99,141],"class_list":["post-1080","post","type-post","status-publish","format-standard","hentry","category-tech","tag-datatype","tag-default","tag-examples","tag-go","tag-golang","tag-sidetoc","tag-type","tag-zero"],"yoast_head":"\nDefault Zero Value of all Types in Go (Golang) With Examples - Welcome To Golang By Example<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/golangbyexamples.com\/go-default-zero-value-all-types\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Default Zero Value of all Types in Go (Golang) With Examples - Welcome To Golang By Example\" \/>\n<meta property=\"og:description\" content=\"Default Value Table Type Default Value Integer 0 Float 0 Complex Number 0 Real and 0 Imaginary Part Byte 0 Rune 0 String “” Bool false Array Every array value to its...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/golangbyexamples.com\/go-default-zero-value-all-types\/\" \/>\n<meta property=\"og:site_name\" content=\"Welcome To Golang By Example\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-04T15:06:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-01-04T17:24:41+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/golangbyexamples.com\/go-default-zero-value-all-types\/\",\"url\":\"https:\/\/golangbyexamples.com\/go-default-zero-value-all-types\/\",\"name\":\"Default Zero Value of all Types in Go (Golang) With Examples - Welcome To Golang By Example\",\"isPartOf\":{\"@id\":\"https:\/\/golangbyexamples.com\/#website\"},\"datePublished\":\"2020-01-04T15:06:55+00:00\",\"dateModified\":\"2020-01-04T17:24:41+00:00\",\"author\":{\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f\"},\"breadcrumb\":{\"@id\":\"https:\/\/golangbyexamples.com\/go-default-zero-value-all-types\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/golangbyexamples.com\/go-default-zero-value-all-types\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/golangbyexamples.com\/go-default-zero-value-all-types\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/golangbyexamples.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Default Zero Value of all Types in Go (Golang) With Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/golangbyexamples.com\/#website\",\"url\":\"https:\/\/golangbyexamples.com\/\",\"name\":\"Welcome To Golang By Example\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/golangbyexamples.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\/\/golangbyexamples.com\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Default Zero Value of all Types in Go (Golang) With Examples - Welcome To Golang By Example","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/golangbyexamples.com\/go-default-zero-value-all-types\/","og_locale":"en_US","og_type":"article","og_title":"Default Zero Value of all Types in Go (Golang) With Examples - Welcome To Golang By Example","og_description":"Default Value Table Type Default Value Integer 0 Float 0 Complex Number 0 Real and 0 Imaginary Part Byte 0 Rune 0 String “” Bool false Array Every array value to its...","og_url":"https:\/\/golangbyexamples.com\/go-default-zero-value-all-types\/","og_site_name":"Welcome To Golang By Example","article_published_time":"2020-01-04T15:06:55+00:00","article_modified_time":"2020-01-04T17:24:41+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/golangbyexamples.com\/go-default-zero-value-all-types\/","url":"https:\/\/golangbyexamples.com\/go-default-zero-value-all-types\/","name":"Default Zero Value of all Types in Go (Golang) With Examples - Welcome To Golang By Example","isPartOf":{"@id":"https:\/\/golangbyexamples.com\/#website"},"datePublished":"2020-01-04T15:06:55+00:00","dateModified":"2020-01-04T17:24:41+00:00","author":{"@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f"},"breadcrumb":{"@id":"https:\/\/golangbyexamples.com\/go-default-zero-value-all-types\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/golangbyexamples.com\/go-default-zero-value-all-types\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/golangbyexamples.com\/go-default-zero-value-all-types\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/golangbyexamples.com\/"},{"@type":"ListItem","position":2,"name":"Default Zero Value of all Types in Go (Golang) With Examples"}]},{"@type":"WebSite","@id":"https:\/\/golangbyexamples.com\/#website","url":"https:\/\/golangbyexamples.com\/","name":"Welcome To Golang By Example","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/golangbyexamples.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/golangbyexamples.com\/author\/admin\/"}]}},"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paOs1b-hq","amp_validity":null,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/1080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/comments?post=1080"}],"version-history":[{"count":17,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/1080\/revisions"}],"predecessor-version":[{"id":1106,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/1080\/revisions\/1106"}],"wp:attachment":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/media?parent=1080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/categories?post=1080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/tags?post=1080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}