Variables and Types

View saved

Declare with var

Name the type after the variable. Zero values apply when you omit an initializer.

package main

import "fmt"

func main() {
	var name string = "Go"
	var lessons int
	lessons = 18
	fmt.Println(name, lessons)
}

Use short declaration

Inside functions, := declares and infers the type from the value.

count := 3
ready := true
fmt.Println(count, ready)

Common basic types

  • int, int64, float64
  • string for text
  • bool for true/false
  • byte is an alias for uint8

Constants

Constants are declared with const and cannot change.

const MaxLessons = 20
fmt.Println(MaxLessons)

Comments

One comment per signed-in account. Comments are saved with this page’s URL.