KalyanChakravarthy.net

Thoughts, stories and ideas.

Go-lang Notes

Declarations

Sometimes its hard to believe that this is indeed a compiled language, mostly due to type inference. In most cases you do not need to specify type in declarative initialization unless you are just declaring.

var someInt int; // type required
anotherInt := 23 // type not required

Type inference is only available inside functions and not in top level declarations. The designers state that it is by design to optimise for faster compilation.

Arrays & Maps

Coming from python and objective-c, I always took for granted that I can dump in any type of object into either an array or a dictionary/map; This is not something you can do in Go and I initially presumed this might become an obstacle. I was surprised to note that I felt no need for it at all, yet.

The syntax seemed complicated initiaially, but its its quite simple if you know how go synatax goes

// Declaring
var Mapping map[string]string;
Mapping = make(map[string]string)

// string maps to string
var Dictionary := make(map[string]string)

// String maps to *Route
var RouteMapping := make(map[string]*Route)

Types

Everything outside the realm of simple functions almost always involves types. They have to be used for

  1. structs (for oop-ish things)

    type TinyServer struct {}
    
  2. aliases a.k.a typedefs

    type TinyConnectionHandler func(*TinyConnection)
    
  3. interfaces

    type TinyServerHandler interface {}
    

Structs/OOP

Golang is not particularly object oriented. It is possible to achieve the notion of it using structs, associated methods and composition.

Composition Example:

type Connection struct {
    ResponseWriter http.ResponseWriter
    Request  *http.Request
}

type TinyConnection struct {
    Connection
    Url *url.URL
    Vars map[string]string
}

This makes all methods as well as variables from Connection available in TinyConnection struct. Trying to override a method will cause a compile time error.

Associated Methods:

func (connection *Connection) WriteString(content string) {
    fmt.Fprintf(connection.ResponseWriter, content)
}

At first look the syntax may look quite odd especially if you are coming from other languages like python. But I found it to be quite similar to C++, with the exception of position of return which in Go is at the end.

:::cpp
void Connection::WriteString(char *string){
    //blah
}

Interfaces

Another oddity of golang is how interfaces are implemented - You declare an interface and its contents, but you never explicitly specify if a struct/class implements an interface. Consider this example:

:::go
type Handler interface {
    ServeHTTP(ResponseWriter, *Request)
}

type MyHandler struct {}
func (MyHandler *h) ServeHTTP(ResponseWriter, *Request) {}

The struct MyHandler is assumed to implement the interface Handler just because it implements ServeHTTP method. It almost feels like its duck typing, although attempting to assign handler directly will cause compile error.

:::go
type Server struct {
    handler Handler
}

myHandler := &MyHandler{}
server := &Server{}
server.handler = myHandler // compiler error

The only way to do the above assignment is to create a new instance of the interface and assign the pointer value

handler := new(Handler)
*handler = myHandler
server.handler = *handler

Regexp

An amusing aspects of the regexp module is that the method naming conforms to a regex. Straight from godoc:

There are 16 methods of Regexp that match a regular expression and identify the matched text. Their names are matched by this regular expression:

Find(All)?(String)?(Submatch)?(Index)?

Multiple returns

functions can return multiple values. This although is part of the language, its not very different from returning a tuple in python

:::python
def returnTwoThings():
    return (1,2)    
a, _ = returnTwoThings()

Go

:::go
func returnTwoThings() (int,int) {
    return (1,2)
}
a, _ := returnTwoThings()

Only major difference is in go, the return types have to be explicitly declared. This means one can't arbitrarily decide to return 3 items unlike python.

Main

Go does not compile to a binary executable unless there is a main package and there exists a main() function.

Garbage Collection

The language is designed to be garbage collected. So the binary ships with a GC. Although this is great if the final main() is in go, its not useful if you'd like to use your go code to work with something written and compiled C.

In short: You can import C code into Go. The vice versa is not possible, yet. There goes my dream of building a mac/ios app in golang.