Skip to content

Instantly share code, notes, and snippets.

@davidklassen
Created September 6, 2016 15:18
Show Gist options
  • Save davidklassen/f85d6bb9bfc943aa41d1f64ebc405ab3 to your computer and use it in GitHub Desktop.
Save davidklassen/f85d6bb9bfc943aa41d1f64ebc405ab3 to your computer and use it in GitHub Desktop.

Development environment with golang and docker

Install git

https://git-scm.com/downloads

Make sure that git binary is in your PATH environment variable.

Install golang

https://golang.org/dl/

Make sure that go/bin directory is in your PATH environment variable. Set the GOPATH environment variable to point to your workspace directory.

Test golang installation

Now you should be able to compile a simple go program. With any text editor create a hello.go file with the following content:

package main

import "fmt"

func main() {
        fmt.Println("Hello, World!")
}

To compile the program run the following command in the terminal:

$ go build hello.go

This will create an executable, run it to print "Hello, World!":

$ ./hello

Setup the IDE

Download IntellijIDEA from https://www.jetbrains.com/idea/

Install Go plugin and create a new project in $GOPATH/src/github.com/<yourname>/hello

You will be asked to configure the Go SDK while setting up a project.

Using third-party library

Let's create a simple http server using "github.com/gorilla/mux" routing library.

Create a main.go file in your new project with the following content:

package main

import (
	"fmt"
	"github.com/gorilla/mux"
	"net/http"
	"log"
)

func main() {
	router := mux.NewRouter()
	router.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "Hello, World!")
	})
	log.Fatal(http.ListenAndServe(":8888", router))
}

IDE will show that gorilla/mux is not installed, place your cursor on "github.com/gorilla/mux" line and press Alt+Enter, it will suggest installing a package. After that you should be able to run it from the Run menu.

TODO: Docker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment