Variables

Reading material

A Tour of Go: Variables, Variables with initializers, Short variable declarations. Go by Example: Variables, Constants.


First exercise

// variables1
// Make me compile!

// I AM NOT DONE
package main

import "fmt"

func main() {
	var = 5
	fmt.Printf("x has the value %d", x)
}

Second exercise


// variables2
// Make me compile!

// I AM NOT DONE
package main

import "fmt"

func main() {
	x = 5
	fmt.Printf("x has the value %d", x)
}

Third exercise


// variables3
// Make me compile!

// I AM NOT DONE
package main

import "fmt"

func main() {
	var x
	fmt.Printf("x has the value %d", x)
}

Fourth exercise


// variables4
// Make me compile!

// I AM NOT DONE
package main

import "fmt"

func main() {
	x := "TEN" // Don't change this line
	fmt.Printf("x has the value %s", x)

	if true {
		x = 1
		fmt.Println(x + 1)
	}

	fmt.Println(x)
}

Fifth exercise

// variables5
// Make me compile!

// I AM NOT DONE
package main

import "fmt"

const Pi

func main() {
	fmt.Println(Pi)
}

Sixth exercise


// variables6
// Make me compile!

// I AM NOT DONE
package main

import "fmt"

const x = 10

func main() {
	fmt.Println(x)

	x = x + 1
	fmt.Println(x)
}


If something is wrong in this page, edit this page on GitHub