Go: Variables

Photo by Mitchell Luo on Unsplash

Overview of Go

Declaring Variables

package mainimport (     "fmt")func main() {     var i int}
package mainimport (     "fmt")func main() {     var i int
i = 16
fmt.Println(i)
}// Terminal command to run this program is go run main.goOutput: 16

Other Ways to Declare Variables

package mainimport (     "fmt")func main() {     var i int = 16     fmt.Println(i)}// Terminal command to run this program is go run main.goOutput: 16
package mainimport ("fmt")func main() {     i := 16     fmt.Println(i)}// Terminal command to run this program is go run main.goOutput: 16

When to use each type of variable declaration

package mainimport (      "fmt")func main() {// multi-line approach: useful for for loops     var i int     i = 14// single-line: useful for when you need control over the data type     var j float32 = 15// colon-equals: least verbose, use when Go's compiler is able to determine data type     k := 16     fmt.Println(i)// print statements for displaying value and type     fmt.Printf("%v, %T", j, j)     fmt.Printf("%v, %T", k, k)}Output: 14
15, float32
16, int

Block Variables

package mainimport "fmt"var (     playerName    string  = "Andre Agassi"     sponsor       string  = "Head"     worldRanking  int     = 1     stringTension float32 = 65.5)var (     matchesPlayed int  = 0)func main() {     fmt.Println(playerName, sponsor, worldRanking, stringTension, 
matchesPlayed)
}Output:Andre Agassi Head 1 65.5 0

Redeclaring Variables and Shadowing

package mainimport "fmt"
var i int = 27
func main() { var i int = 42 i := 13 fmt.Println(i)}
package mainimport "fmt"var i int = 27func main() {     fmt.Println(i)

var i int = 42
fmt.Println(i)}Output: 27
42
package mainimport "fmt"//uppercase: globally visiblevar I int = 27// lowercase: visible to the package onlyvar (     playerName    string  = "Andre Agassi"     sponsor       string  = "Head"     worldRanking  int     = 1     stringTension float32 = 65.5)func main() {// block: only visible within the main function     var i int = 42     fmt.Println(i)     fmt.Println(I)     fmt.Println(playerName, sponsor, worldRanking, stringTension)}Output:42
27
Andre Agassi Head 1 65.5

Converting Data Types

package mainimport "fmt"func main() {     var i int = 42     fmt.Printf("%v, %T\n", i, i)     var j float32     // conversion operator     j = float32(i)     fmt.Printf("%v, %T\n", j, j)}Output:42, int
42, float32
package mainimport ("fmt""strconv")func main() {     var i int = 42     fmt.Printf("%v, %T\n", i, i)     var j string     // use strconv package's str converter function to change from
an integer to a string
j = strconv.Itoa(i) fmt.Printf("%v, %T\n", j, j)}Output:42, int
42, string

Constants

package mainimport "fmt"
func main() { const firstConst int = 1 fmt.Printf("%v, %T\n", firstConst, firstConst)}Output:1, int
package mainimport (     "fmt")func main() {     const m = 10     var n int16 = 25     fmt.Printf("%v, %T\n", m+n, m+n)}Output:35, int16
package mainimport (     "fmt")const (     _ = iota     doctor     lawyer     developer     dogSitter     accountant)func main() {     var occupationType int = lawyer     fmt.Printf("%v\n", occupationType == lawyer)     fmt.Printf("%v\n", lawyer)}Output:True
2

Learning Materials

Resources

--

--

Software Engineer — Full Stack, JavaScript, ReactJS, Ruby on Rails, OO Programming (https://www.matthewsedlacek.com/)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Matthew Sedlacek

Software Engineer — Full Stack, JavaScript, ReactJS, Ruby on Rails, OO Programming (https://www.matthewsedlacek.com/)