Skip to content

Instantly share code, notes, and snippets.

@Gonzih
Last active May 11, 2018 17:51
Show Gist options
  • Save Gonzih/fcdb78919dbc2ddd6295afa9024848ee to your computer and use it in GitHub Desktop.
Save Gonzih/fcdb78919dbc2ddd6295afa9024848ee to your computer and use it in GitHub Desktop.
Go workshop

GO workshop - CLI for news.ycombinator.com

Configuring your editor

Getting started with go itself

Installing: brew install go

Create main.go file:

package main

import "fmt"

func main() {
	fmt.Println("Happy hacking!")
}

Simply run go run main.go

Task at hand

  • Grab 10 top stories
  • Print title, rating, author to cli of each story
  • Write some tests

Easy extra things

  • Allow to select a particular story in the list
  • Display couple of the comments for the selected story
  • Allow to go back to the story list

Funky extra things

Highly recommended to use only stdlib available modules. But no strict rules. Wanna go crazy and pull in dozens of deps? Go for it! vgo get is your friend.

How-to things

Imports that you are gonna need

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
)

Doing http get request

https://golang.org/pkg/net/http/

resp, err := http.Get("http://example.com/")
if err != nil {
	// handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
// body is gonna have []byte type

Parsing json

https://golang.org/pkg/encoding/json/

Into an array:

ids := make([]int, 0)
err := json.Unmarshal(b, &ids)

Into a struct:

type myStruct struct {
	Title string `json:"title"`
	Author string `json:"author"`
}

func main() {
	var body []byte
	var data myStruct
	....
	json.Unmarshal(body, &data)
}

Printint stuff

https://golang.org/pkg/fmt/

fmt.Printf("This is how you print stuff %s", "in go")

Writing tests

https://golang.org/pkg/testing/

package main

import "testing"

func TestMain(t *testing.T) {
	main()
}

Taking n items from an array

subarray := array[0:10]

Sorting a slice/array

https://golang.org/pkg/sort/#Slice

HN Api

Top Stories: https://hacker-news.firebaseio.com/v0/topstories.json

Item: https://hacker-news.firebaseio.com/v0/item/16814671.json

Full api documentation: https://github.com/HackerNews/API/blob/master/README.md

Dependencies

If you decided for some reason to include external dependencies you can do it in 2 ways:

  • go get github.com/author/project
  • vgo get github.com/author/project

Vgo is the new versioned dependency tool for golang. go get will just pull the dependency master branch in to your $GOPATH (default $GOPATH value is $HOME/go), for the sake of this workshop it should also work fine if you dont want to get distracted by dependency managment tools.

VGO

Getting started with vgo (if you are planning to use external dependencies only):

go get -u golang.org/x/vgo

export PATH=$PATH:$HOME/go/bin

echo > go.mod
echo 'module "/home/myuser/go/src/workshop-project-name"' > go.mod # is optional
vgo build
./workshop-project-name

Now you can replace go command with vgo

Fix for vgo requires Go 1.10 but VGOROOT=/usr/lib/go is not a Go 1.10 source tree:

sudo mkdir /usr/lib/go/api
sudo touch /usr/lib/go/api/go1.10.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment