Skip to content

Latest commit

 

History

History
120 lines (84 loc) · 3.8 KB

README.md

File metadata and controls

120 lines (84 loc) · 3.8 KB

Travis status

First thing first: install Golang

Follow the instructions here:

Download the repository in your GOPATH

Git clone the repo using this command:

$ git clone https://github.com/aubm/Golang-Codelab $GOPATH/src/github.com/aubm/golang-codelab

Then open the project folder with Gogland. Opening the first Go file, you might have to specify the Go SDK that you want to use.

Setup GOROOT

Build and run your first Go program

Jump into the demo folder:

$ cd $GOPATH/src/github.com/aubm/golang-codelab/demo

This demo program prints "Hello" followed by a random name in the standard output. Run it with this command:

$ go get ./...
$ go run main.go
Hello James Smith

go get ./... analyses all the third-party dependencies of the program and attempts to automatically download them.

go run compiles and runs the code in a temporary folder. If you just want to compile without running it, use this command:

$ go build

This will generate a new binary file in the current directory named demo, or demo.exe on Windows. Try and execute it with this command:

$ ./demo
Hello Anthony Moore

or on Windows:

$ ./demo.exe
Hello Ethan Smith

It should be noted that this particular file is a self-contained executable binary file. Which means that, unlike other technologies like Java, NodeJS or PHP, you can drop it on another compatible machine and run it, without the need for a Go runtime to be installed on that machine.

Cross compilation

If you work on Linux and you compile a Go program using go build, it will generate a statically linked binary file that can only be executed on a Linux machine that have the same architecture as the one you use.

Fortunately, Go has support for cross compilation. This means that even working on Linux, you can easily generate an executable file for Windows or Mac, using this command:

$ env GOOS=windows GOARCH=amd64 go build
$ ls
demo.exe main.go

Let's have a look at this demo program source code

package main

import (
	"fmt"

	"github.com/Pallinder/go-randomdata"
)

func main() {
	someRandomName := randomdata.FullName(0)
	fmt.Println("Hello " + someRandomName)
}
  • The code starts with the declaration of the package name. Here main is a special case that indicates that this particular package can be built in order to generate an executable file. Any other name would have made this code to be considered as a library.
  • Then there is a list of imported dependencies. Each imported package exposes its api in an implicitly created variable, named after the name of the package. It is important to note that only the package members whose name starts with a capital letter are considered part of the package api. So that FullName() is "public", but fullName() would be "private". The correct terminology simply is "exported" or "non exported". As we will see, the same rule applies to a structure properties. This may seem a little odd but it turns out to be quite efficient!
  • Then there is the main function. Like for the package name, the main function is special because it will automatically be invoked by the runtime at program bootstrap.

Now let's actually get started

First follow the instructions here to get through the basics.

Then go to the workshops section to get to the real stuff :)

Going further