Basic Concepts Flashcards

1
Q

Packages

A

Programs start running in package main.
package main

import (
“fmt”
“math/rand”
)

func main() {
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Imports

A
import (
    "fmt"
    "math"
)
-OR-
import "fmt"
import "math"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Exported names

A

After importing a package, you can refer to the names it exports.

In Go, a name is exported if it begins with a capital letter.

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

Functions

A
func add(x int, y int) int {
    return x + y
}
-OR-
When two or more consecutive named function parameters share a type, you can omit the type from all but the last.
func add(x, y int) int {
    return x + y
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Multiple results

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
6
Q

Named results

A

If the result parameters are named, a return statement without arguments returns the current values of the results.

func split(sum int) (x, y int) {
    x = sum * 4 / 9
    y = sum - x
    return
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Variables

A

var x, y, z int

var c, python, java bool

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

Variables with initializers

A

A var declaration can include initializers, one per variable.

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

var x, y, z int = 1, 2, 3
var c, python, java = true, false, “no!”

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

Short variable declarations
:=
Inside or outside available?

A

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

Outside a function, every construct begins with a keyword (var, func, and so on) and the := construct is not available.

func main() {
    var x, y, z int = 1, 2, 3
    c, python, java := true, false, "no!"
fmt.Println(x, y, z, c, python, java) }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Go’s basic types are

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
11
Q

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.

const Pi = 3.14

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

Numeric Constants

A

Numeric constants are high-precision values.

An untyped constant takes the type needed by its context.

const (
Big = 1 &laquo_space;100
Small = Big&raquo_space; 99
)

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

For Loop

A

Go has only one looping construct, the for loop.

The basic for loop looks as it does in C or Java, except that the ( ) are gone (they are not even optional) and the { } are required.

sum := 0
for i := 0; i < 10; i++ {
    sum += i
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

For continued

A

As in C or Java, you can leave the pre and post statements empty.

sum := 1
for ; sum < 1000; {
    sum += sum
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

For is Go’s “while”

A

At that point you can drop the semicolons: C’s while is spelled for in Go.

sum := 1
for sum < 1000 {
    sum += sum
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Forever

A

for {

}

17
Q

if statement

A

The if statement looks as it does in C or Java, except that the ( ) are gone and the { } are required.

    if x < 0 {
        return sqrt(-x) + "i"
    }
18
Q

If with a short statement

A

Like for, the if statement can start with a short statement to execute before the condition.

Variables declared by the statement are only in scope until the end of the if.

if v := math.Pow(x, n); v < lim {
    return v
}
19
Q

If and else

A

Variables declared inside an if short statement are also available inside any of the else blocks.

func pow(x, n, lim float64) float64 {
    if v := math.Pow(x, n); v < lim {
        return v
    } else {
        fmt.Printf("%g >= %g\n", v, lim)
    }
    // can't use v here, though
    return lim
}