Go
Objectives
Built by Google engineers, the objectives are:
Easy to write, similar to a scripting language;
Easy to learn from another language such as C, Python, ...
Compiled language for fast execution;
Modern language, supporting concurrency, designed for the web of tomorrow.
Variables declaration
Go is a typed language. You can specified the type of a variable, or let the compiler decide for you:
var a int
a = 1
var b int = 2
// var keyword is optional only if you use the := syntax
c := 3
A variable can't change its type in its scope.
Functions
Functions declaration
func functionName(parameterName type) returnType {
// code
}
Example:
func add(a int, b int) int {
return a + b
}
If parameters have the same type, you can write it only once:
func add(a, b int) int {
return a + b
}
For public functions, you must start the name with a capital letter. Example: fmt.Println().
Functions return
A function can return multiple values:
func swap(a, b int) (int, int) {
return b, a
}
Anonymous functions
Anonymous functions are functions without a name. They are also called function literals. They are useful for:
Inline function declarations:
func main() {
func() {
fmt.Println("Hello")
}()
// Output: Hello
}
Assigning a function to a variable:
func main() {
f := func() {
fmt.Println("Hello")
}
f()
// Output: Hello
}
Passing a function as a parameter to another function:
func main() {
f := func() {
fmt.Println("Hello")
}
func(f func()) {
f()
}(f)
// Output: Hello
}
Closures:
// Returns a function that returns an int
func fibonacci() func() int {
first := 0
second := 1
f := func() int {
sum := first + second
first, second = second, sum
return sum
}
return f
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
Iterations
There is no
while
loop in Go. You can only usefor
loops:
for i := 0; i < 10; i++ {
// code
}
For an infinite loop:
for {
// code
}
Conditions
No parenthesis around conditions:
if condition {
// code
} else if condition {
// code
} else {
// code
}
We can also declare a variable in the condition:
if a := 1; a == 1 {
// code
}
There is also a
switch
statement, with nobreak
needed:
switch a {
case 1:
// code
case 2:
default:
// code
}
There is no ternary operator in Go (
condition ? true : false
). The reason is that Go chose the simple approach, and the ternary operator is less readable for beginners.
Packages
In Go, our code is organized in packages. A package is a collection of files that are compiled together. A package can be imported by another package.
The main package is the entry point of our program. It is the package that will be executed when we run our program.
References
Stéphane Karraz, Go instructor at ESGI
Last updated