Post

Go variables

Understanding Go variable declarations, type conversions between int and float32, and converting integers to strings with strconv.

This example shows how to declare variables in Go using both the var keyword and the short declaration operator :=. It also demonstrates package-level variable blocks, type conversion between int and float32, and converting an integer to a string using strconv.Itoa. Note that in Go, declared but unused variables cause a compile error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main

import (
	"fmt"
	"strconv"
)

// if declaring vatiable over here
// you can't use a := 10
var (
	a int = 42
	actorName string = "Elisabeth Salden"
	companion string = "Sarah Elisabeth Salden"
	// example of acronym
	theHTTP string = "https://google.com"
	doctorNumber int = 11
	season int = 10

)

var I int = 30

func main()  {
	// var i int
	var i int = 40
	var j int = 30
	var x float32 = 10

	var g float32
	g = float32(j)
	// if variable is declared and not used -> you will get a compile error
	k := i + j

	var s string
	s = strconv.Itoa(i)


	fmt.Println("Hello from Go!", k)
	fmt.Printf("Hello from Go! %v %T\n", k, k)
	fmt.Printf("Float number! %v %T\n", x, x)
	fmt.Printf("This variable was converted to float32: %v, %T\n", g, g)
	fmt.Printf("Converting int -> string: %s, %T\n", s, s)
}
This post is licensed under CC BY 4.0 by the author.