A Tour of Go - Basics: Packages, variables, and functions. Flashcards

1
Q

Go programs are made up of …

A

packages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which package do Go programs start in?

A

main

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

By convention, the package name is the …

A

last element of the import path

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Give an example of a package statement?

A
package main
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Give and example of an import statement?

A
import "fmt"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Give and example of a factored import statement?

A
import (
    "fmt"
    "math/rand"
)

Factored import statements are considered good style.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is an exported name?

A

In Go, and exported name begins with a capital letter.

When importing a package you can only refer to the exported names.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Give an example of a function that adds two integers?

A
func add(x, y int) int {
    return x + y
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Function can return multiple results

Give an example of a function that swaps two strings?

A
func swap(x, y string) (string, string) {
    return y, x
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a naked return?

A

Go’s return values can be named. If so, they are treated as variables defined at the top of the function.

A return statement with no arguments will return the names return values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Describe the var statement.

A

The var statement declares a list of variables, the type is last.

A var statement can be used at package or function level.

var i, j int
var s string
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an initaliser?

A

A var declaration can include initialisers, one per variable.

If an initialiser is present, the type can be omitted; the variable will take the type of the initialiser.

var i int = 1
var s = "hello"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a short variable declaration?

A

In functions, the := short assignment statement can be used in place of a var declaration with implicit type.

i, j := 1, 2
s := "hello"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are Go’s basic types?

A
bool

string

int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32
     // represents a Unicode code point

float32 float64

complex64 complex128
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a zero value?

A

Variables declared without an explicit initial value are given their zero value.

The zero value is:

  • 0 for numeric types,
  • false for the boolean type, and
  • ”” (the empty string) for strings.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a type conversion?

A

The expression T(v) converts the value v to the type T.

var i int = 1
var f float64 = float(i)
17
Q

What is type inference?

A

When declaring a variable without specifying an explicit type (either by using the := syntax or var = expression syntax), the variable’s type is inferred from the value on the right hand side.

18
Q

What are constants?

A

Constants are declared like variables, but with the const keyword.

Constants can be character, string, boolean, or numeric values.

Constants cannot be declared using the := syntax.

19
Q

What’s special about numeric constants?

A

They are high precision values.

const Big = 1 &laquo_space;100