Golang - General Flashcards

1
Q

What are the core characteristics of Go?

A
  • Fast compilation
  • Fully compiled
  • Strongly typed
  • Concurrent by default
  • Garbage collected
  • Simplicity as a core value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Go good at?

A
  • Web services
  • Web applications
  • Task automation
  • GUI / Thick-client
  • Machine learning
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What looks like a hello world in Go?

A

package main

import (
“fmt”
)

func main() {
	fmt.Println("Hello, playground")
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What every Go source code file have at the to ?

A

The package name

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

What the anatomy of a Go source file ?

A
  • The package name
  • The import block
  • The code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to make comments in Go ?

A
Using // at the beginning of the line like:
// your comment

Or using /* / tag which permit to span. a comment on multiple lines like:
/

Your Comment
*/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
Does this code block is valid ?
func main()
{
	fmt.Println("Hello, playground")
}
A
No because Go add a semi-colon at the end of every line. In that case it will give:
func main();
{;
	fmt.Println("Hello, playground");
};
And it means there is function without a body.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What character should be use for identation in Go?

A

The tab

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

How to get documentation about a package/object/method?

A

Using go doc like:
go doc json.Decoder.Decode
go doc json.decoder.decode
go doc json.decode

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

Give the different ways to declare and initialize a variable?

A
The three way to do it:
        //Declaration + Initialization (Separate)
	var i int
	i = 42
	//Declaration + Initialization (Simultaneous)
	var f float32 = 3.14
	fmt.Println(f)
	// Implicit initialization syntax
	firstName := "Nicolas"
	fmt.Println(firstName)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a pointer data type variable in Go?

A

It’s a variable which hold the address to a place in memory which hold the value, instead of holding the value directly.

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

How to declare a constant ?

A
Like this :
// Implicit declaration syntax
const c = 3
or this:
// Explicit declaration syntax
const c int = 3

In the first case the type will be determine each time the constant is used.
In the second one it will be fixed at the declaratioon
time.

We have to initialize constant at the time we declare it. And the value need to be know at compile time.

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

What is “iota” keyword ?

A
iota is used in constant block. each time that keywork is used in a constant block, the value is increment by 1, like:
const (
     first = iota
     second = iota
)
or
const (
     first = iota
     second
)

Gives:
0 1

The value is reset between the const block. For example:
const (
     first = iota
     second = iota
)
const (
     third = iota
)

Gives:
0 1 0

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

How to declare and initialize an array?

A
// Long syntax
// Declaration 
var arr [3]int
// Initialization
arr[0] = 1
arr[1] = 2
arr[2] = 3
// Short Syntax
arr2 := [3]int{1,2,3}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to declare and initialize a slice?

A
//Long syntax
var slice []int
slice = append(slice, 1,  2, 3)
// Short Syntax
slice2 := []int{1, 2, 3}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What prints the folowing code ?
package main

import (
“fmt”
)

func main() {
      slice := []int{1, 2, 3}
      slice = append(slice, 8, 9, 10)
      fmt.Println(slice[1:])
      fmt.Println(slice[:2])
      fmt.Println(slice[1:2])
}
A

[2 3 8 9 10]
[1 2]
[2]

17
Q

How to declare and initialize a map?

A
//Long syntax
var m map[string]int
m = make(map[string]int)
m["foo"] = 42
//Short syntax
m := map[string]int{"foo":42}
18
Q

How to delete a element from a map ?

A

m := map[string]int{“foo”:42}

delete(m, “foo”)

19
Q

How to declare and initialize a struct?

A
// Define a struct
type user struct {
     ID int
     FirstName string
     LastName string
}
// Declare a struct
var u user
// Inialize a struct
u.ID = 1
u.FirstName = "Arthur"
u.LastName = "Dent"
//Short version for declare and initialize a struct
u2 := user{ ID: 1, Firstname: "Arthur", LastName: "Dent"}
//Short multiline for declare and initialize a struct
u2 := user{ ID: 1,
             FirstName: "Arthur",
             LastName: "Dent",
             }
20
Q

Give me an example of arithmetic pointer in go.

A

There is no pointer arithmetic in go

21
Q

Give me:

  • the pointer operator
  • the derference operator
  • the “adress of” operator
A

*
*
&

22
Q

How to declare and initialize a pointer?

A

var firstName *string = new(string)

*firstName = “Arthur”

23
Q

How to define a function ?

A
// Without parameters
func startWebServer() {
	fmt.Println("Starting server...")
	// do important things
	fmt.Println("Server started")
}
// With parameters
func startWebServer(port int, numberOfRetries int) {
	fmt.Println("Starting server...")
	// do important things
	fmt.Println("Server started on port", port)
	fmt.Println("Number of retries", numberOfRetries)
}
// With parameters shorter version
func startWebServer(port, numberOfRetries int) {
	fmt.Println("Starting server...")
	// do important things
	fmt.Println("Server started on port", port)
	fmt.Println("Number of retries", numberOfRetries)
}
// With return value
func startWebServer(port, numberOfRetries int) error {
	fmt.Println("Starting server...")
	// do important things
	fmt.Println("Server started on port", port)
	fmt.Println("Number of retries", numberOfRetries)
	return errors.New("Something went wrong") 
}
// With return values
func startWebServer(port, numberOfRetries int) (int, error) {
	fmt.Println("Starting server...")
	// do important things
	fmt.Println("Server started on port", port)
	fmt.Println("Number of retries", numberOfRetries)
	return port, errors.New("Something went wrong")
}
24
Q

What the pupose of the “write-only” variable ?

A

That kind of variable materialized by “” is usefull if a function return multiples values. If you don’t want to use one of this values you can use “” to retreive that value, that value will be ignore.
Remember that if you want to use a materialized variable to receive that value but you don’t used that variable in the rest of you code, your program will not compile.

25
Q

Enumerate the different kind of loop.

A

Go only have on type of loop, it’s the “for” loop.

26
Q

What the different kind of “for” loop ?

A
  • Loop till condition
  • Loop till condition with post clause
  • Infinite loops
  • Loop over collections
27
Q

How for loop is constructed ?

A
// Simple one
var i int
for i < 5 {
	println(i)
	i++
}
// Break loop flow
var i int
for i < 5 {
	println(i)
	i++
	if i == 3 {
		break
	}
}
//Finish the current iteration
var i int
for i < 5 {
	println(i)
	i++
	if i == 3 {
		continue
	}
	println("Continuing...")
}
// Post clause
var i int
for ; i < 5; i++ {
	println(i)
}
println(i)
// Infinite loop, ugly sintax
var i int
for ; ; {
	if i > 5 {
		break
	}
	println(i)
	i++
}
// Infinite loop, this is the way
var i int
for {
	if i > 5 {
		break
	}
	println(i)
	i++
}
// Collection loop Ugly way
slice := []int{1, 2, 3}
for i := 0; i < len(slice); i++ {
	println(slice[i])
}
// Collection loop This is the way
slice := []int{1, 2, 3}
for i, v := range slice {
	println(i, v)
}
28
Q

What the pupose of “panic” key word?

A

Panic is used to indicate an unrecoverable state of the app, and make it exits, it will print additional information for debugging purposes.

29
Q

What the construct of the different if statements?

A
// Simple
if a == b {
}
// If/else
if a == b {
} else {
}
// if/else if
if a == b {
} else if b == c {
}
30
Q

What the construct of a switch/case statement ?

A
switch os := runtime.GOOS; os {
case "darwin":
	fmt.Println("OS X.")
case "linux":
	fmt.Println("Linux.")
default:
	fmt.Printf("%s.\n", os)
}
31
Q

What the syntax to remove an element from a slice ?

A
var strSlice = []string{"India", "Canada", "Japan", "Germany", "Italy"}
var index = 1
strSlice = append(strSlice[:index], strSlice[index+1:]...)