1

Getting Started with Go 👨🏽‍💻

GO-Development-Banner.jpg

What is Go?

  • Go is a compiled programming language, created by Google. It is known for being an alternative to C/C++, with an more readable syntax.

Hello World

  • Go needs the main-function. It gets executed when running the programm. A unique package name must be provided, there must be one main package.
package main

import "fmt"

funcmain() {
  fmt.Println("Hello World!")
}

Execute & Build

  • The build-file, which can be executed without having Go installed, depends on the OS. On Windows, it will be an .exe.
go run app.go// run the code in dev mode
go build app.go// compiling
./app.go// executing the build-file

Variables

What is special about Variables in Go?

  • In Go, there can no undefined or null values occur, when declaring variables.
  • There is no need to declare a variable with a data type.
  • We can declare multiple variables at once.
  • There is a long way, providing the datatype when declaring, and a shorthand, using type inference for declaring.
  • We can use var and const. Last one means constant, so changing the value of such a variable, will break the program.
    A constant must have an initial value.

Declaring Variables

  • Using type inference, Go can guess the correct datatype for our variable. We can declare multiple variables at once.
// Providing the value later
var name string 
name = "Max"// Providing the value fist
var name string = "Max"// Using type inference
name := "Max"
var width, height float32 = 2.5, 6.1// or: width, height := 2.5, 6.1
fmt.Println(width) 	// 2.5
fmt.Println(height)	// 6.1

Default value

  • When providing a datatype for the variable when declaring, but leaving out the value, a default value will be applied.
var firstVar bool
fmt.Println(firstVar) // falsevar firstVar string
fmt.Println(firstVar) // ""var firstVar float64
fmt.Println(firstVar) // 0var firstVar int 
fmt.Println(firstVar) // 0

Functions in Go

Function & Return

  • The datatype stands after the variable name. The last ‘int’ is the datatype of the return-value.
funcadd(x int, y int) int {
	return x + y
}

funcmain() {
	fmt.Println(add(5, 5))  // 10
}

Shorting the datatypes

  • If both parameters have the same data type, we can use a shortcut.
funcadd(x, y int)int {return x + y
}

Multiple Returns

  • A function in Go can return multiple things.
funcperson(name string, age int)(string, int) {return name, age + 1
}

funcmain() {
	name, age := person("max", 22)
	fmt.Println(name, age)
	// max23
}

Arrays

Basic Array

  • Arrays can only hold one type of data. The first element always has the index of 0. They have a fixed size. Like variables in Go, they hold default values.
var ages [5]int 
fmt.Println(ages)
// [0 0 0 0 0]

Adding some values to it

var ages [5]int
ages[0] = 21
ages[1] = 23
fmt.Println(ages)
// [21 23 0 0 0]// the better way:ages := [5]int{23, 21, 19, 23, 24}
fmt.Println(ages)
// [23 21 19 23 24]

Slices

  • Since arrays have a fixed number of elements, this is problem when we want to add something. By leaving out the number of elements, we initialize a slice.
ages := []int{23, 21, 19, 23, 24}
ages = append(ages, 27)
// append does not edit the existing slice. // It returns a new one.
fmt.Println(ages)
// [23 21 19 23 24 27]

Looping through an Array

ages := [3]int{23, 21, 19}
for index, value :=range ages {
    fmt.Println(index, value)
    // 0 23// 1 21// 2 19
}

Conditionals

If-Else

  • There are now brackets in the syntax.
if5 > 3 {
    fmt.Println("5 > 3")
} else {
    fmt.Println("3 > 5")
}

Else-if

  • Else-If provides another condition, and gets executed if the condition is true, and if is not getting executed.
if5 > 5 {
    fmt.Println("5 > 3")
} elseif5 >= 5 {
    fmt.Println("Greater / equal")
} else {
    fmt.Println("3 > 5")
}

Variables in if-else

  • We can declare a variable within the syntax of an if-statement.
if age := 9; age < 18 {
    fmt.Print("Not grown up")
 }

For Loop

Which Loops exist?

  • In Go, there is only the for-loop available. But it can be used as while for example, as you can see in the examples.

for-loop

  • No () arround the content of the loop.
funcmain() {for i := 0; i < 5; i++ {
		fmt.Println(i)
	}
}

“While”-Loop in Go

for i := 0; i < 5; {
    fmt.Println(i)
    // endless prints
}

Pointers

Using a Pointe

  • The syntax with the ‘&’ returns the memory address of the variable. Therefore, a Pointer holds a memory address.
funcmain() {
    var age = 24var agePointer = &age
	fmt.Println(agePointer)
	// for example '0xc000016078'
}

Getting the value behind the Pointer

  • We can use the ‘*’ syntax to read the value, saved in the memory with the memory address, the pointer is holding.
var age = 24var agePointer = &age
fmt.Println(*agePointer) // 24

Changing values with Pointers

  • Not only can we read out values from the memory, we can also change it through pointers.
var age = 24var agePointer = &age
*agePointer++
fmt.Println(*agePointer) // 25

Map & Struct

Basic Struct

type person struct {
	name string
	age int
}
func main() {
	personMax := person{name: "Max", age: 21}
	fmt.Println(personMax) // { Max21}
	fmt.Println(personMax.name) // Max
}

Basic Map

  • In the [] stands the type of the key, follwing is the type of the value.
people := make(map[string]int)
people["Max"] = 23
people["Tom"] = 19

fmt.Println(people["Max"])

Looping through a Map

people := make(map[string]int)
people["Max"] = 23
people["Tom"] = 19for key, value := range people {
    fmt.Println(key, value)
    // Max 23// Tom 19
}```

**_Happy Coding!_** 👽
Escribe tu comentario
+ 2