<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Go Basics Training</title><link>/</link><description>Recent content on Go Basics Training</description><generator>Hugo -- gohugo.io</generator><language>en</language><atom:link href="/index.xml" rel="self" type="application/rss+xml"/><item><title>Footer</title><link>/pdf/footer/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/pdf/footer/</guid><description> /</description></item><item><title>Header</title><link>/pdf/header/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/pdf/header/</guid><description>- acend gmbh</description></item><item><title>Variables</title><link>/docs/basics/variables/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/basics/variables/</guid><description>Basics Go is a statically typed language. This means that each variable gets a type on declaration which can&amp;rsquo;t be changed later.
Commonly used data types are:
int and uint float32 and float64 bool string byte (alias for uint8) error to return errors from functions All Go&amp;rsquo;s predeclared identifiers are defined in the builtin package.
Declaration The short assignment statement := declares a variable and assigns a value to it. The type of variable is inferred from the value (type inference).</description></item><item><title>Flow control</title><link>/docs/basics/flow-control/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/basics/flow-control/</guid><description>If Else Conditionals in Go are similiar to other programming languages. Notice that there are no round brackets surrounding the condition.
1 2 3 4 5 6 7 8 9 10 11 12 package main import &amp;#34;fmt&amp;#34; func main() { x := 10 if x &amp;gt;= 5 { fmt.Println(&amp;#34;X is greater or equal to 5&amp;#34;) } else { fmt.Println(&amp;#34;X is smaller than 5&amp;#34;) } } Output: X is greater or equal to 5 Multiple logical conditions can be combined with &amp;amp;&amp;amp; (AND) and || (OR).</description></item><item><title>Functions</title><link>/docs/basics/functions/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/basics/functions/</guid><description>Basics The func keyword declares a function.
In the following example we declare the function add. It takes two parameters a and b of type int and returns a value of type int which is the sum of a and b.
1 2 3 4 5 6 7 8 9 10 11 12 package main import &amp;#34;fmt&amp;#34; func add(a int, b int) int { return a + b } func main() { result := add(2, 3) fmt.</description></item><item><title>Pointers</title><link>/docs/basics/pointers/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/basics/pointers/</guid><description>Basics In addition to the basic data types Go also have pointers. A pointer contains a memory address of an actual value or nil if they do not point to anything. The zero value of a pointer is nil.
Pointer types are indicated with a star. For example:
The type *int is a pointer to an int The type *bool is a pointer to a bool Concerning pointers you mainly have to remember two operations.</description></item><item><title>Structs</title><link>/docs/basics/structs/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/basics/structs/</guid><description>Basics Structs are used to group related variables called fields. In that respect structs in Go are similar to classes or objects in other languages.
Declare Struct Type This statement declares a new struct type called User. This struct contains three fields. The name of the user (string), the number of failed login attempts (int) and if the user is locked (bool):
type User struct { Name string FailedLogins int Locked bool } Create Instance A new struct instance can be created using a struct literal:</description></item><item><title>Slices</title><link>/docs/basics/slices/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/basics/slices/</guid><description>Basics With a slice literal (e.g. []int{1, 2, 3}) and the short assignment we we can initialize a new slice. Slices store multiple items of the same type.
1 2 3 4 5 6 7 8 9 package main import &amp;#34;fmt&amp;#34; func main() { q := []int{2, 3, 5, 7, 11, 13} fmt.Println(q) fmt.Println(&amp;#34;Length of slice:&amp;#34;, len(q)) } Output: [2 3 5 7 11 13] Length of slice: 6 Slice of structs We can also store multiple instances of a struct.</description></item><item><title>Maps</title><link>/docs/basics/maps/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/basics/maps/</guid><description>Basics A map maps keys to values. In other languages it is also called hash map or dictionary. The following example shows how to:
initialize a map with an empty map literal set a value by key get a value by key delete a key get the length of the map 1 2 3 4 5 6 7 8 9 10 11 12 13 package main import &amp;#34;fmt&amp;#34; func main() { m := map[string]int{} m[&amp;#34;john&amp;#34;] = 66 fmt.</description></item><item><title>Interfaces</title><link>/docs/basics/interfaces/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/basics/interfaces/</guid><description>Basics Interfaces are used to express general behaviour across multiple types. Interface types are defined as a set of method signatures. All types which implement this set of methods implement the defined interface.
As an example we define the interface Stringer:
type Stringer interface { String() string } All types that implement the method String() and return a string implement the Stringer interface. Types implicitly implement an interface if they implement the required methods.</description></item><item><title>Project Structure</title><link>/docs/project-structure/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/project-structure/</guid><description>Basics A Go project usually consists of exactly one module. Every directory within the project is a package. Hence a module is a collection of packages.
So usually we can say:
Module = Project = Git Repository Package = Directory Module path To create a new Go project in the current directory we initialize a new module by running go mod init &amp;lt;module-path&amp;gt;. If we want to create a new project named myproject we would do the following steps:</description></item><item><title>Testing</title><link>/docs/testing/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/testing/</guid><description>Writing Tests Tests are defined as functions in the following form:
func TestXxx(t *testing.T) The tests reside in the same directory as the source code. The test files end with _test.go. The code in these files is not compiled into the binary when building the project. For demonstration purposes we put the function and test in the same code block. These are usually in a different file (e.g. calculator.go and calculator_test.</description></item><item><title>Input/Output</title><link>/docs/standard-library/io/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/standard-library/io/</guid><description>Basics 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package main import ( &amp;#34;fmt&amp;#34; &amp;#34;log&amp;#34; &amp;#34;os&amp;#34; ) func main() { const filename = &amp;#34;/tmp/file.txt&amp;#34; err := os.WriteFile(filename, []byte(&amp;#34;Hello, file system\n&amp;#34;), 0644) if err != nil { log.Fatal(err) } content, err := os.ReadFile(filename) if err != nil { log.Fatal(err) } fmt.Printf(&amp;#34;%s&amp;#34;, content) } Output: Hello, file system Reader/Writer The above example of reading a file loads the whole file into memory.</description></item><item><title>JSON</title><link>/docs/standard-library/json/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/standard-library/json/</guid><description>Encoding Go allows us to encode structs to json by using json.Marshal .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package main import ( &amp;#34;encoding/json&amp;#34; &amp;#34;fmt&amp;#34; &amp;#34;os&amp;#34; ) type User struct { Name string FullName string Followers int } func main() { user := User{ Name: &amp;#34;Alice&amp;#34;, FullName: &amp;#34;Alice Nyffenegger&amp;#34;, Followers: 44, } output, err := json.</description></item><item><title>HTTP Client</title><link>/docs/standard-library/http-client/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/standard-library/http-client/</guid><description>The Go standard library offers a HTTP package which provides a server and a client. In the following sections we learn how we can use the HTTP client.
Quick start The following example shows how we can perform a GET request and print the body to the standard output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package main import ( &amp;#34;fmt&amp;#34; &amp;#34;io&amp;#34; &amp;#34;net/http&amp;#34; &amp;#34;os&amp;#34; ) func main() { resp, err := http.</description></item><item><title>HTTP Server</title><link>/docs/standard-library/http-server/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/standard-library/http-server/</guid><description>Quick start Go allows us to start a simple HTTP server with minimal code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package main import ( &amp;#34;fmt&amp;#34; &amp;#34;net/http&amp;#34; &amp;#34;os&amp;#34; ) func helloHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, &amp;#34;Hello World&amp;#34;) } func main() { http.HandleFunc(&amp;#34;/hello&amp;#34;, helloHandler) err := http.ListenAndServe(&amp;#34;:8080&amp;#34;, nil) if err != nil { fmt.Println(err) os.Exit(1) } } Now you can access http://localhost:8080/hello .</description></item><item><title>Date and Time</title><link>/docs/standard-library/time/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/standard-library/time/</guid><description>Basics Time is represented with the time.Time struct.
The output in the examples below is generated by the Go playground . The Go playground uses a static time to achieve optimal caching performance.
1 2 3 4 5 6 7 8 9 10 11 package main import ( &amp;#34;fmt&amp;#34; &amp;#34;time&amp;#34; ) func main() { time := time.Now() fmt.Println(time) } Output: 2009-11-10 23:00:00 &amp;#43;0000 UTC m=&amp;#43;0.000000001 Parsing To parse a string to time.</description></item><item><title>Concurrency</title><link>/docs/concurrency/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/concurrency/</guid><description>Goroutines To run multiple functions concurrently we can start Goroutines. You can think of a Goroutine as a lightweight thread managed by the Go runtime.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package main import ( &amp;#34;fmt&amp;#34; &amp;#34;time&amp;#34; ) func print(word string) { for i := 0; i &amp;lt; 5; i++ { fmt.Println(word) time.Sleep(100 * time.Millisecond) } } func main() { go print(&amp;#34;hello&amp;#34;) print(&amp;#34;world&amp;#34;) } Output: world hello world hello hello world world hello hello world When the main function returns, the program exits.</description></item><item><title>Generics</title><link>/docs/generics/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/generics/</guid><description>The Go 1.18 release (February 2022) adds support for generics. In the following section, we will take a look at the new language feature but we will not cover every detail.
Why do we need Generics Imagine you implement a function contains to check wheter a certain item is in a list:
func contains(list []int, item int) bool { for _, current := range list { if current == item { return true } } return false } The function above only works for slices of the type int.</description></item><item><title>Packaging</title><link>/docs/packaging/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/packaging/</guid><description>Basics Go binaries are statically linked. That means all dependencies are included within the binary.
go build main.go The binary will only work on the same platform that built it (e.g. 64bit Linux). Go allows us to easily generate binaries compatible with other architectures:
GOOS=linux GOARCH=arm64 go build main.go GOOS=windows GOARCH=amd64 go build main.go Docker The Dockerfile uses multi-stage builds so that the resulting image is small and secure. The image is built with the full Golang Docker image and the resulting binary is copied into the Distroless image .</description></item><item><title>ASCII Pyramid</title><link>/docs/exercises/lab_ascii_pyramid/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/exercises/lab_ascii_pyramid/</guid><description>Task Create a function which prints an ASCII Pyramid. The function must take the height of the pyramid as parameter.
If you call the function with 5 as parameter we should get the following pyramid:
* *** ***** ******* ********* Tips Standard library packages The package fmt contains various print functions.
Standard library functions With fmt.Print you can print strings:
// print space fmt.Print(&amp;#34; &amp;#34;) // print newline fmt.Print(&amp;#34;\n&amp;#34;) Calculate number of spaces and stars On each line you have to print the appropriate number of spaces and stars:</description></item><item><title>Number Guessing Game</title><link>/docs/exercises/lab_number_guessing_game/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/exercises/lab_number_guessing_game/</guid><description>Task Write a number guessing game. At the start create a random number. Then ask the user to enter a number on the command line (standard input) until the user guesses the correct number.
If the user enters the string exit the program should exit.
The output could look like in the following example. In the example the user enters the numbers 5, 7 and 6.
guess number between 0 and 9 guess number: 5 wrong number.</description></item><item><title>HTTP Client</title><link>/docs/exercises/lab_http_client/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/exercises/lab_http_client/</guid><description>Overview We will write a small CLI tool that gets the follower count and the full name of a specific Github user.
Information about a Github user we can obtain from the Github REST API under https://api.github.com/users/&amp;lt;user&amp;gt;. Besides many other information a call to https://api.github.com/users/mitchellh returns the following information:
{ &amp;#34;login&amp;#34;: &amp;#34;mitchellh&amp;#34;, ... &amp;#34;name&amp;#34;: &amp;#34;Mitchell Hashimoto&amp;#34;, ... &amp;#34;followers&amp;#34;: 9973, ... } The Github API limits the number of unauthenticated requests per IP to 60 per hour.</description></item><item><title>HTTP Server</title><link>/docs/exercises/lab_http_server/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/docs/exercises/lab_http_server/</guid><description>Part 1: Ping and Logger Tasks Run a server and implement a handler which returns pong on the endpoint /ping. Create a middleware to log every request. Log the path, method, duration and the IP of the client of the request. Your log should look similar to this:
2022/04/11 10:03:08 remote=192.168.1.143 path=/foo method=GET duration=13.765874ms Tips See 5.4. HTTP Server.
Standard library packages Consider using the log package from the standard library You can measure time using the time package: Measure duration start := time.</description></item></channel></rss>