<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Labs on Go Basics Training</title><link>/docs/</link><description>Recent content in Labs on Go Basics Training</description><generator>Hugo -- gohugo.io</generator><language>en</language><atom:link href="/docs/index.xml" rel="self" type="application/rss+xml"/><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>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></channel></rss>