Home

Awesome

<div align="center">

Learn Golang

go-lang-intro-image

Learn Golang to build simple, reliable, and efficient software.

</div>

Contents

Why?

Go is a great programming language if you want a good balance between simplicity, beginner-friendliness (being able to find/train new team members) and high performance.

What?

Go is a statically typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. Go is syntactically similar to C, but with memory safety, garbage collection, structural typing, and CSP-style concurrency The language is often referred to as "Golang" because of its domain name, golang.org, but the proper name is Go. ~ https://en.wikipedia.org/wiki/Go_(programming_language)

Who?

Anyone who wants to broaden their programming skills should learn Go. The syntax is immediately familiar to anyone who has written a C-style language such as JavaScript.

How?

You can learn the basics of Go online: https://play.golang.org But when it comes time to actually run a program on your computer, you will need to install the compiler. Visit: https://golang.org/doc/install and select the installer for your operating system.

Hello World!

Following: https://golang.org/doc/install#testing

Create a new file called hello.go and type the following code in it:

package main

import "fmt"

func main() {
	fmt.Printf("hello, world\n")
}

Once you've saved the file, in your terminal, compile the programming with the following command:

go build hello.go

That will create an executable file called hello (with no extension) To execute the program, run the command:

./hello

In your terminal, you should see:

hello, world

Congratulations you just wrote your first Go program!

<br />

Learning Resources

If you just want to know the basics, watch Jake Wright's "Learn Go in 12 Minutes": https://youtu.be/C8LgvuEBraI

image

If you want to learn Go in more depth, FreeCodeCamp Learn Go Programming Tutorial for Beginners: https://youtu.be/YS4e4q9oBaU

image

The tutorial is almost 7 hours long but covers quite a lot.

Recommended Reading

Popularity?

Despite the simplicity and performance of Go, it's nowhere near as used/known as Google would like you to believe. For example it's not in the Top 10 languages on GitHub: https://octoverse.github.com/#top-languages <img width="1171" alt="github-most-popular-languages" src="https://user-images.githubusercontent.com/194400/87814203-2d8cc200-c85b-11ea-8229-c5d1b4dae2d4.png">

And it's used by fewer than 9% of developers (who answered the survey) on StackOverflow. https://insights.stackoverflow.com/survey/2020#technology <img width="802" alt="stackoverflow-dev-survey-languages" src="https://user-images.githubusercontent.com/194400/87814783-4c3f8880-c85c-11ea-9b2a-c7e1c068491e.png">

Don't let these stats dissuade you. The biggest reason Go is not as popular as JavaScript, Python or PHP is simply because the other languages got a massive head start.

Go usually scores well on synthetic benchmarks: https://www.techempower.com/benchmarks We know from experience that Postgres is usually the bottleneck in our Apps, so the programming language doesn't matter quite as much you think. What matters is having a sane approach to concurrency and channels for building the features your people want.

Why Not?

Common complaints/gripes developers have with Go include:

We feel all the issues (with the exception of error handling) are actually features of Go that lead to simpler more maintainable code.

As noted in "Why Elixir" > Go is our 3rd Choice for programming languages. We use Elixir because Phoenix Channels makes Real Time easy and Nerves makes building, deploying and maintaining IoT a breeze. If Elixir didn't exist it would be a choice between Rust and Go. Go is a lot simpler and beginner-friendly. Rust is "safer" and more performant.

Imports

This code groups the imports into a parenthesized, "factored" import statement.

You can also write multiple import statements, like:

import "fmt"
import "math"

Or

import (
	"fmt"
	"math/rand"
)

Packages

Every Go program is made up of packages.

Programs start running in package main.

This program is using the packages with import paths "fmt" and "math/rand".

package main

import (
	"fmt"
	"math/rand"
)

func main() {
	fmt.Println("My favorite number is", rand.Intn(10))
}

Basic types

Go's basic types are:

The example shows variables of several types, and also that variable declarations may be "factored" into blocks, as with import statements.

package main

import (
	"fmt"
	"math/cmplx"
)

var (
	ToBe   bool       = false
	MaxInt uint64     = 1<<64 - 1
	z      complex128 = cmplx.Sqrt(-5 + 12i)
)

func main() {
	fmt.Printf("Type: %T Value: %v\n", ToBe, ToBe)
	fmt.Printf("Type: %T Value: %v\n", MaxInt, MaxInt)
	fmt.Printf("Type: %T Value: %v\n", z, z)
}

The int, uint, and uintptr types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems. When you need an integer value you should use int unless you have a specific reason to use a sized or unsigned integer type.

Variables

The var statement declares a list of variables; as in function argument lists, the type is last.

A var statement can be at package or function level. We see both in this example.

package main

import "fmt"

var c, python, java bool

func main() {
	var i int
	fmt.Println(i, c, python, java)
}

Variables with initializers

A var declaration can include initializers, one per variable.

If an initializer is present, the type can be omitted; the variable will take the type of the initializer.

package main

import "fmt"

var i, j int = 1, 2

func main() {
	var c, python, java = true, false, "no!"
	fmt.Println(i, j, c, python, java)
}

Short variable declarations

Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type.

Outside a function, every statement begins with a keyword (var, func, and so on) and so the := construct is not available.

package main

import "fmt"

func main() {
	var i, j int = 1, 2
	k := 3
	c, python, java := true, false, "no!"

	fmt.Println(i, j, k, c, python, java)
}

Functions

A function can take zero or more arguments.

In this example, add takes two parameters of type int.

Notice that the type comes after the variable name.

package main

import "fmt"

func add(x int, y int) int {
	return x + y
}

func main() {
	fmt.Println(add(42, 13))
}

Functions continued

When two or more consecutive named function parameters share a type, you can omit the type from all but the last.

In this example, we shortened

x int, y int

to

x, y int

Multiple results

A function can return any number of results.

The swap function returns two strings.

package main

import "fmt"

func swap(x, y string) (string, string) {
	return y, x
}

func main() {
	a, b := swap("hello", "world")
	fmt.Println(a, b)
}

Zero values

Variables declared without an explicit initial value are given their zero value.

package main

import "fmt"

func main() {
	var i int
	var f float64
	var b bool
	var s string
	fmt.Printf("%v %v %v %q\n", i, f, b, s)
}

The zero value is: