Introduction Go

Table of Contents

home

1. Basic commands

1.1. How do we run the code in our project?

1.1.1. go run

  • It compiles the file and execute it
    • go run main.go

1.1.2. go build

  • It generates an executable file, just that, it does not execute it.

1.1.3. go fmt

  • It is go format (fmt), it is useful to format all the code in each file in the current directory.

1.1.4. go install / go get

  • go install compiles and installs a package, go get downloads the raw source code of someone else’s package.

1.1.5. go test

  • Runs any tests associated with the current project

1.2. What does ’package main’ mean?

Files in the same package can freely call functions defined in other files.

1.2.1. Executable package

  • the word main is a keyword, we use it only when we are makin a package that we want to spit out some reasonable file.
    • package main:
      • defines a package that can be compiled and then executed, must have a func called ’main’

1.2.2. Reusable package

  • Package abcxyz:
    • defines a package that can be used as a dependency

1.3. What does ’import “ftm”’ mean?

ftm contains input and output functions, it is like System.Out.println.

1.4. What is that func thing?

It is a short of function, it is a function like any other programming language.

1.5. How is the main.go file organized?

package main package declaration
import “fmt” import other packages that we need
func main(){ ftm.Println(“hi there”)} Declare functions, tell Go to do things

2. Variables

2.1. Declare a variable

Go is a typed programming language, variables must first be initialized with the ’:=’ operator or the ’var variableName type’ syntax, so there are a few ways to declare a variable:

  • var card string = “Ace of spades”
  • card := “Ace of spades”
    • :=
      it is only used to declare or inicialize a variable, after we can use = to assigned another value, take into account that the type of value once is declared can not be changed, in this case always is gonna be string.

3. Data structures

3.1. slices

They are dinamyc data structures.

3.2. Arrays

They are fixed data structures.

4. Iterate

for i, card := range cards {
        fmt.Println(i, card)
    }

Author: Andres Amezquita

Created: 2023-04-20 jue 20:11