Printing and variables

Question Click to View Answer

What does the following code print to the console?

package main

import "fmt"

func main() {
    fmt.Println("hi there")
}

"hi there" is printed to the console.

If this script is stored in a hi.go file it can be executed with go run hi.go.

Executable commands must always use package main.

import "fmt" imports the fmt package. This allows you to call the Println() function that's defined in fmt and print values to the console.

The go run command runs the main() function.

What does the following code print?

fmt.Println(5)

5 is printed.

The fmt.Println() function can be used to print integers, strings, and other types of values.

What does the following command line command return when run in the terminal?

godoc fmt Println

The command returns documentation on the fmt.Println() function.

func Println(a ...interface{}) (n int, err error) Println formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered

What does the following code print to the terminal?

var x string = "hi"
fmt.Println(x)

hi is printed to the console.

The variable x is assigned to the string "hi". The fmt.Println() function is used to print the x variable to the console.

What does the following code print to the terminal?

var num string = 5
fmt.Println(num)

This code throws the following error: cannot use 5 (type int) as type string in assignment.

The num variable is typed as a string, so it can only be assigned to string values. It cannot be assigned to 5, which is an integer (int) value.

What does the following code print to the terminal?

var coolNum int
coolNum = 8
fmt.Println(coolNum)

8 is printed to the terminal.

The coolNum variable is first declared as an int variable.

Then coolNum is assigned to the value 8.

Finally, the fmt.Println() function is used to print coolNum.

What does the following code print to the terminal?

var goat string
goat = "mj"
goat = "lebron"
fmt.Println(goat)

lebron is printed to the console.

The goat variable was initially assigned to the "mj" string. The goat variable was then reassigned to the "lebron" string.

Variables can be reassigned in go.

What does the following code print to the terminal?

gretzkyNum := 99
fmt.Println(gretzkyNum)

The gretzkyNum variable is assigned to the value 99.

In Go, := is for variable declaration and assignment.

Go infers the gretzkyNum type to be int based on the value.

What does the following code print to the terminal?

myName := "matthew"
myName = 888
fmt.Println(myName)

This code throws the following error: cannot use 888 (type int) as type string in assignment.

The myName variable is initially assigned to "matthew" and declared as a string.

myName = 888 causes an error to be thrown because the myName variable is declared as a string and cannot be reassigned to an integer value.

What does the following code print to the terminal?

myDinner = "hot dog"
fmt.Println(myDinner)

This code throws the following error: undefined: myDinner.

The myDinner variable is assigned, but it's not declared with a type.

This code works if := is used.

myDinner := "hot dog"
fmt.Println(myDinner)

It also can work if the variable type is explicitly declared.

var myDinner string = "hot dog"
fmt.Println(myDinner)

What does the following code print to the terminal?

const Pi = 3.14
fmt.Println(Pi)

3.14 is printed to the terminal.

const stands for constant.

The constant Pi is assigned to the value 3.14. Constants cannot be declared using the := syntax.

What does the following code print to the terminal?

const Greeting = "Hello!"
Greeting = "Hola!"
fmt.Println(Greeting)

This code throws the following error: cannot assign to Greeting.

Go constants cannot be reassigned.