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}