Practical intro to Go language

The “Hello world”

1
2
3
4
5
6
package main
import "fmt"

func main(){
fmt.Println("Hello world")
}

Package = Workspace = Project

Several files in go can belong to a same package. All of them must have the package main on top of the file.

Two types of packages

  • Executable -> Generates a file that we can actually run
  • Reusable -> A good place to put reusable logic such as “helpers” or libraries or dependencies.

The word main is used for executable packages.

1
$> go build --> makes our exec file

Imports

Use to get code from other packages, imports go right below the package name.

“fmt” or Format is one of the most known packages and lets us print to stdout among other things. For more info refer to golan.org/pkg/fmt

Go file organization

  1. package name
  2. imports
  3. functions

Functions

We’ve already seen how a function looks on the “Hello world” example. In the function below we have between brackets the parameters and next to them the type that the function will return. Additionally, we are casting the sum of the integers we receive by parameter to string in order to return the correct type.

1
2
3
func functionName(a, b, c int) string {
return string(a + b + c)
}
  • The main() function is called automatically
  • Functions must have return types specified.

Function names that starts with lower case are functions that can’t be used outside the file/package and functions that start with upper case can be exported and used outside the file/package

Invariant arguments

Go functions can take invariant arguments. Let’s see an example

1
2
3
4
5
6
7
func my_functions (i int, s string, fn func (u int), t ...string) {
fmt.Println(i)
fn(8)
for a, b := range t {
fmt.Println("%d - %s", a , b)
}
}

As we can see we can receive n strings in the argument t.

Variable declarations

There are several ways of declaring a variable

1
2
var card string = "I'm  a wonderful string"
[declaration] [name] [type] = [value]

A shorter way where the compiler interprets the type

1
card := "I'm a  wonderful string"

Slices

Go has arrays but those are hardly used. Unlike arrays, slices are typed only by the elements they contain (not the number of elements). These are dynamically allocated while arrays have a fixed length.

Declare an empty array with make, fill it and print it by accessing it.

1
2
3
4
5
6
s := make([]string, 3),
s[0] = "a"
s[1] = "b"
s[2] = "c"
fmt.Println("set:", s)
fmt.Println("get:", s[2])

The built-in append, which returns a slice containing one or more new values. Note that we need to accept a return value from append as we may get a new slice value.

1
2
3
s = append(s, "d")
s = append(s, "e", "f")
fmt.Println("apd:", s)

Declare and define a slice in the same line

1
mySlice := []string{"A","B","C"}

Slices can also be copy’d. Here we create an empty slice c of the same length as s and copy into c from s.

1
2
3
c := make([]string, len(s))
copy(c, s)
fmt.Println("cpy:", c)

Slices also support a “slice” operator with the syntax slice[low:high].

1
2
l := s[2:5]
fmt.Println("sl1:", l)

Intro: https://github.com/contre95/notes/blob/master/Digitales/Apuntes/Coding/Go/1-Intro.md
Slices: https://github.com/contre95/notes/blob/master/Digitales/Apuntes/Coding/Go/2-Slices.md


Author: Lucas Contreras