Golang – The HardWay

Standard

About golang :

Go, also commonly referred to as golang, is a programming language developed at Google[7] in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson.[6] It is a statically-typed language with syntax loosely derived from that of C, adding garbage collection, type safety, some dynamic-typing capabilities, additional built-in types such as variable-length arrays and key-value maps, and a large standard library.

Source: https://en.wikipedia.org/wiki/Go_(programming_language)

I’m starting to use golang language in some of Onebit’s projects.  I have learn this language before, but only for introduction, something like “Hello world”.  This time, I’m learning it deeper, and just as the title said, learning golang is hard.

I will try to jump straight to golang’s language features like :

  1. Functional / High Order Function
  2. Closures
  3. Multiple Return Values
  4. Named multiple return values
  5. Basic variable
  6. Struct
  7. Struct concept from other languages
  8. Interface

Actually, golang has other advanced features like :

  1. Go routines. Golang’s way to manage lightweight thread.
  2. Channel.  How golang manage send and receive values, something like Actor model, but a lot simpler.

There are basic types in Go :

  1. bool
  2. string
  3. int, int8, int16, int32, int64
  4. uint uint8 uint16 uint32 uint64 uintptr
  5. byte // alias for uint8
  6. rune // alias for int32
  7. float32 float64
  8. complex64 complex128

You can learn from here: http://tour.golang.org/basics/11

How do I setup my environment for golang ? There is a differences when i first time setup my environment for golang.  We must set $GOPATH environment variable, and this variable used for many go’s projects and codes.  This is the folder structure:

.
├── bin
│   ├── benchcmp
│   ├── callgraph
│   ├── digraph
│   ├── eg
│   ├── fiximports
│   ├── gocode
│   ├── godef
│   ├── godex
│   ├── goimports
│   ├── golint
│   ├── gomvpkg
│   ├── gorename
│   ├── gotype
│   ├── html2article
│   ├── oracle
│   ├── present
│   ├── ssadump
│   ├── stress
│   ├── stringer
│   ├── tipgodoc
├── pkg
│   └── linux_amd64
└── src
├── 9fans.net
├── github.com
├── go-codes
├── golang.org
├── gopkg.in
├── go-rabbit

We have to put our go codes inside <em>src folder.  So this mean, our project must be put in the same structure with go’s core library language, it’s different with other languages, like PHP or Ruby when we can put our project codes anywhere in the system separated with core language libraries.

The go tool is designed to work with open source code maintained in public repositories. Although you don’t need to publish your code, the model for how the environment is set up works the same whether you do or not.

Building codes in golang means you should be ready to working in the procedural ways, i mean the real procedural!

Procedural programming is a programming paradigm, derived from structured programming, based upon the concept of the procedure call. Procedures, also known as routines, subroutines, methods, or functions (not to be confused with mathematical functions, but similar to those used in functional programming), simply contain a series of computational steps to be carried out.

So, if you came from strong OOP paradigm like Java, your process to get used to golang will probably become harder. The question is, can I create something like class in go ? Unfortunately, there are no class in go, but the good news, you can still manage your codes like class in go.  Remember prototype in JavaScript ?

package main

import (
“fmt”
“math”
)

type Vertex struct {
X, Y float64
}

func (v *Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func main() {
v := &Vertex{3, 4}
fmt.Println(v.Abs())
}

Source : http://tour.golang.org/methods/1

The method’s concept in go, for me it’s like prototype in JavaScript.

Maybe the last question is how do we run our codes ? Golang is compiled language.  You must compile your codes and then run it, but you don’t need to compile many times in development, you can run your go’s codes using :

go run main.go // if you only have single file in your projects or maybe

or

go run *.go

For the last, this is my slide about golang’s language features :

Leave a Reply