DEV Community

Cover image for Starting with Go
Nathan Ferreira
Nathan Ferreira

Posted on • Edited on

Starting with Go

Starting with Go

This is a brief tutorial.

Go is a statically typed, compiled high-level programming language and open source programming language created by Google.

Golang was born out of a need to be more performative. Differs from other languages by being compiled, highly scalable and simplistic.

Features:

  • Syntactically similar to C, but with memory safety, garbage collection, structural typing, and CSP-style concurrency.
  • Built-in facilities and library support for writing concurrent programs.
  • Uses goroutines, a type of light-weight process, for concurrency.
  • Uses channels for sending messages between goroutines and implementing concurrency control structures.
  • Automatically standardized code formatting and style with gofmt tool.
  • Rules enforced by Go encourage a particular explicit, concrete, and imperative programming style.

Use cases:

  • Cloud & Network Services: With a strong ecosystem of tools and APIs on major cloud providers, it is easier than ever to build services with Go.
  • Command-line Interfaces: With popular open source packages and a robust standard library, use Go to create fast and elegant CLIs.
  • Web Development: With enhanced memory performance and support for several IDEs, Go powers fast and scalable web applications.
  • DevOps & Site Reliability: With fast build times, lean syntax, an automatic formatter and doc generator, Go is built to support both DevOps and SRE.

Go has a memory model describing how goroutines must use channels or other operations to safely share data. The existence of channels sets Go apart from actor model-style concurrent languages like Erlang. Go does not provide any built-in notion of safe or verifiable concurrency, but idiomatic concurrent programs prefer channels.

Creators: Rob Pike and Ken Thompson


Installing Go

Windows

Tutorial to install on windows

Linux

Tutorial to install asdf

execute the commands below:

asdf plugin-add golang

asdf install golang

asdf global golang {{version of golang}}

asdf shell golang {{version of golang}}

Mac

Tutorial to install Homebrew
execute the command below:
brew install go


Primitive types

We have string, int, uint, bool, rune, complex and byte.

1) Int

Is one of the numeric types which represents a set of integers. There are various kind of integer types associated int. We have uint or unsigned integers, complex numbers, and int itself. Along with them are the different sets of numbers that they contain. For example, int8 contains all the 8-bit integers from -128 to +127, int16 contains all the 32-bit integers from -32768 to +32767.

By default, when we write int, the bits associated with it depends on the architecture of your computer. It can be either 32 or 64 bits.

2) Uint — Unsigned Int

Unsigned means no signal. That is means, there are no negative values only positives.

3) String

This type represents a series of characters, used for names and some IDs. String type is usually used when we want to name things out or to write sentences.

4) Bool

Bool is used to tell if it is true or false. In the world of programming is widely used to tell if the user is active, or if the validation of a function is true.

uint8

the set of all unsigned 8-bit integers (0 to 255)

uint16

the set of all unsigned 16-bit integers (0 to 65535)

uint32

the set of all unsigned 32-bit integers (0 to 4294967295)

uint64

the set of all unsigned 64-bit integers (0 to 18446744073709551615)

int8

the set of all signed 8-bit integers (-128 to 127)

int16

the set of all signed 16-bit integers (-32768 to 32767)

int32

the set of all signed 32-bit integers (-2147483648 to 2147483647)

int64

the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

float32

the set of all IEEE-754 32-bit floating-point numbers

float64

the set of all IEEE-754 64-bit floating-point numbers

complex64

the set of all complex numbers with float32 real and imaginary parts

complex128

the set of all complex numbers with float64 real and imaginary parts

byte

alias for uint8 rune alias for int32

Although error is a primitive type, it is an interface type, it is an interface type, which wrappers around the string type.


A simple Hello World

Let’s start creating a folder named hello_world.

Run the following command below so we can boot the Go configuration package.

go mod init hello_world
Enter fullscreen mode Exit fullscreen mode

Let’s write the code bellow

package main

import "fmt"

func main() {
    message := "Hello World"
    fmt.Println(message)
}
Enter fullscreen mode Exit fullscreen mode

Execute the command below to execute the

go run main.go
Enter fullscreen mode Exit fullscreen mode

Image description

Top comments (0)