GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

No menu could be recovered. Follow the blue links in the page.

This view

  • Original page
  • New address

GeezerVison is showing gobyexample.com — Go by Example: Variables

Go by Example: Variables

In Go, variables are explicitly declared and used by the compiler to e.g. check type-correctness of function calls.

package main
import "fmt"
func main() {

var declares 1 or more variables.

    var a = "initial"
    fmt.Println(a)

You can declare multiple variables at once.

    var b, c int = 1, 2
    fmt.Println(b, c)

Go will infer the type of initialized variables.

    var d = true
    fmt.Println(d)

Variables declared without a corresponding initialization are zero-valued. For example, the zero value for an int is 0.

    var e int
    fmt.Println(e)

The := syntax is shorthand for declaring and initializing a variable, e.g. for var f string = "apple" in this case. This syntax is only available inside functions.

    f := "apple"
    fmt.Println(f)
}
$ go run variables.go
initial
1 2
true
0
apple

Next example: Constants.

by Mark McGranaghan and Eli Bendersky | source | license

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.