Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Variables in Go
package main
import "fmt"
var global1 int // Declaring a variable with `var` keyword
var global2 int = 10 // Assigning a value to a variable right away
var global3 = "this is ok!" // Go infer type based on your value
// bad_var := "this doesn't work" // `:=` can be used only inside a function
func main() {
local1 := 9 // Inside a function, Go use type inferrence
// so that you don't have to state type explicitly
var local2 = 5 // This is ok
var local3 = 7 // This is ok too
fmt.Println(global1,
global2,
global3,
local1,
local2,
local3,
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.