Golang - General Flashcards
What are the core characteristics of Go?
- Fast compilation
- Fully compiled
- Strongly typed
- Concurrent by default
- Garbage collected
- Simplicity as a core value
What is Go good at?
- Web services
- Web applications
- Task automation
- GUI / Thick-client
- Machine learning
What looks like a hello world in Go?
package main
import (
“fmt”
)
func main() { fmt.Println("Hello, playground") }
What every Go source code file have at the to ?
The package name
What the anatomy of a Go source file ?
- The package name
- The import block
- The code
How to make comments in Go ?
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
*/
Does this code block is valid ? func main() { fmt.Println("Hello, playground") }
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.
What character should be use for identation in Go?
The tab
How to get documentation about a package/object/method?
Using go doc like:
go doc json.Decoder.Decode
go doc json.decoder.decode
go doc json.decode
Give the different ways to declare and initialize a variable?
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)
What is a pointer data type variable in Go?
It’s a variable which hold the address to a place in memory which hold the value, instead of holding the value directly.
How to declare a constant ?
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.
What is “iota” keyword ?
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 to declare and initialize an array?
// 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 to declare and initialize a slice?
//Long syntax var slice []int slice = append(slice, 1, 2, 3) // Short Syntax slice2 := []int{1, 2, 3}
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]) }
[2 3 8 9 10]
[1 2]
[2]
How to declare and initialize a map?
//Long syntax var m map[string]int m = make(map[string]int) m["foo"] = 42
//Short syntax m := map[string]int{"foo":42}
How to delete a element from a map ?
m := map[string]int{“foo”:42}
delete(m, “foo”)
How to declare and initialize a struct?
// 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", }
Give me an example of arithmetic pointer in go.
There is no pointer arithmetic in go
Give me:
- the pointer operator
- the derference operator
- the “adress of” operator
*
*
&
How to declare and initialize a pointer?
var firstName *string = new(string)
*firstName = “Arthur”
How to define a function ?
// 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") }
What the pupose of the “write-only” variable ?
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.
Enumerate the different kind of loop.
Go only have on type of loop, it’s the “for” loop.
What the different kind of “for” loop ?
- Loop till condition
- Loop till condition with post clause
- Infinite loops
- Loop over collections
How for loop is constructed ?
// 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) }
What the pupose of “panic” key word?
Panic is used to indicate an unrecoverable state of the app, and make it exits, it will print additional information for debugging purposes.
What the construct of the different if statements?
// Simple if a == b { }
// If/else if a == b { } else { }
// if/else if if a == b { } else if b == c { }
What the construct of a switch/case statement ?
switch os := runtime.GOOS; os { case "darwin": fmt.Println("OS X.") case "linux": fmt.Println("Linux.") default: fmt.Printf("%s.\n", os) }
What the syntax to remove an element from a slice ?
var strSlice = []string{"India", "Canada", "Japan", "Germany", "Italy"} var index = 1 strSlice = append(strSlice[:index], strSlice[index+1:]...)