capypad
0 day streak
go / beginner
Snippet

Variable Declarations

In Go, you can declare variables using the 'var' keyword with an explicit type, or the short declaration operator ':=' which infers the type automatically.

snippet.go
go
1
2
3
4
5
6
7
8
9
package main
 
import "fmt"
 
func main() {
var name string = "Gopher"
age := 10
fmt.Printf("%s is %d years old", name, age)
}
Breakdown
1
var name string = "Gopher"
Declares a variable named 'name' of type 'string'.
2
age := 10
Short declaration that infers 'age' is an integer.