Hello World go

Hello World

Compare GO to other AWS scripting languages:

Property  Node.js  Python  Go
Scripting Language  yes  yes no
Files / Program  many many one
Typing  dynamic  dynamic  static
Multi-OS  yes  limited yes

Node.JS an Python are interpreted languages, that means the source code is interpreted at runtime. This means a little bit more flexibility like ducktyping, but usually that means a slower execution. Also you have to have the interpreted itself installed to run a program.

GO compiles programs statically. That means a standalone single executable is created. And since GO 1.16 you can embed files into you binary.

This binary will be compiled for a target OS (Operation System). If you use go programs locally on you machine, then this is the target OS. But you can create running binaries for other OSs on your machine also.

This compilation is so fast, that you also can directly run a go programs.

Exercise: Run Hello World

  1. Start an Editor
  2. Create an empty directory
  3. Create a file named “main.go” in this directory
  4. Save the file
package main

import "fmt"

func main() {
        fmt.Println("go")
}
go run main.go
Line  Meaning
package main  This is the main/root package
import "fmt"  import the library “fmt” = format
func main(){}  the main function gets executed
fmt.Println("go")  Use the function “Println” = print with new line from the “fmt” library
  1. Run this programm
go run main.go

go run will directly run go files.

An output will look like

go run main.go
go

Go has done the compilation and the execution steps.

Exercise: Compile hello world

We can also split the compilation and the execution.

  1. Create “dist” directory For the compiled binary we create a directory dist for distribution:
mkdir dist
  1. Compile Program
go build -o dist/printer main.go

go buildjust compiles a file. Now we have the standalone executable printer in the dist directory.

Let`s run it:

dist/printer
go

You see that this is real fast.

See also

Source

See the full source on github.

Sources