Application 2018-11-13

Summary of Variable Definitions and Declarations in Golang

Learn Go variable declaration patterns: var syntax, short declarations, type inference, and visibility rules for identifiers.

Read in: ja
Summary of Variable Definitions and Declarations in Golang

Overview

A summary of patterns for variable definitions and declarations in Golang.

Cautions in Variable Definitions and Declarations

Variable Definitions and Declarations

Variable declarations

var i int
fmt.Printf("%T", i) // int
var a, b, c string
fmt.Printf("%T", a) // string
fmt.Printf("%T", b) // string
fmt.Printf("%T", c) // string
var s = "Hello World"
fmt.Printf("%T", s) // string
var x, y, z int = 1, 2, 3
fmt.Printf("%T", x) // int
fmt.Printf("%T", y) // int
fmt.Printf("%T", z) // int
var (
   a string
   x int
   y, z = 2, 3
)
fmt.Printf("%T", a) // string
fmt.Printf("%T", x) // int
fmt.Printf("%T", y) // int
fmt.Printf("%T", z) // int
var i, j = 1, 2
fmt.Printf("%T", i) // int
fmt.Printf("%T", j) // int

Short variable declarations

i := 1
fmt.Printf("%T", i) // int
i, j := 1, 2
fmt.Printf("%T", i) // int
fmt.Printf("%T", j) // int

References

Tags: Golang
Share: 𝕏 Post Facebook Hatena
✏️ View source / Discuss on GitHub
☕ Support

If you enjoy this blog, consider supporting it. Every bit helps keep it running!


Related Articles