Tag: go

Go Language, First Steps

Go, first experience.

What

We are living in a very rich IT ecosystem, where developers aren’t afraid to explore new languages, tools and to use out of the box thinking. It’s a great time to living in this IT era! Following this trend, and due to some need I had to start to learn
a new language, GO.

Go, also called golang, was developed at Google. The motivation was to have a language less complex than C++.

Go has great advantages, such as:

  • Concurrency system, far superior than Python, for instance.
  • Compiled language, which makes it faster
  • The executable is static linked, which allows to create an executable for the destination platform. It means, it doesn’t need an interpreter.
  • strongly handling exceptions
  • Low memory footprint
  • Very easy to learn
  • Docker is built in Go

Starting

Installation

Just visit the Golang web site
The binaries are here.

Baby steps

First things first: IDEs

Learning

I started with the “official” tutorial A Tour of Go

The big points:

  • Every Go program is made up of packages
  • Programs start running in package main
  • By convention, the package name is the same as the last element of the import path
  • In Go, a name is exported if it begins with a capital letter
  • In functions, the type comes after the variable name
  • When two or more consecutive named function parameters share a type, you can omit the type from all but the last
  • A function can return any number of results
  • Go’s return values may be named. Also called naked return
  • The var statement declares a list of variables (package or function level)
  • Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type
  • The expression T(v) converts the value v to the type T (casting)
  • Constants are declared with the const keyword
  • loop: for – like c, java, c++ but without the parentesis
  • The while> in Go its the for. for {} = infine loop
  • if doesnt need the parentesis
  • the if> allows a statement before the condition: if v := math.Pow(x, n); condition {
  • switch doesnt have the “break”, it is implicit
  • switch without a condition is the same as switch true. Good for long sequence of “if”
  • A defer statement defers the execution of a function until the surrounding function returns. defer add(2,3) PS: call’s arguments are evaluated immediately
  • Go has pointers!
  • The type *T is a pointer to a T value. Its zero value is nil.
  • The &var operator generates a pointer to its operand.
  • Unlike C, Go has no pointer arithmetic.
  • A struct is a collection of fields “type Complex struct”
  • arrays: var a [10]int
  • slices: var s []int = primes[1:4] – A slice does not store any data, it just describes a section of an underlying array
  • A slice has both a length (number of elements it contains) and a capacity (the number of elements in the underlying array)
  • Slices of slices
  • A map maps keys to values: var m map[string]int
  • Go functions may be closures. return func(x int) int { return x }
  • Go does not have classes. However, you can define methods on types. func (v Vertex) Abs() float64 {}
  • An interface type is defined as a set of method signatures. type Abser interface {Abs() float64}
  • Interfaces are implemented implicitly
  • The empty interface – may hold values of any type
  • A type assertion provides access to an interface value’s underlying concrete value. t := i.(T)
  • ype switches switch v := i.(type) { case T:…
  • Go programs express error state with error values. type error interface {…
  • The io package specifies the io.Reader interface.
  • A goroutine is a lightweight thread managed by the Go runtime. go f(x, y, z)
  • Channels are a typed conduit through which you can send and receive values with the channel operator, <-.
  • Like maps and slices, channels must be created before use: ch :=make(chan int)
  • Channels can be buffered.
  • The select statement lets a goroutine wait on multiple communication operations.

Conclusion

In a first impression I would say its a very good language for scripting. No complications around object oriented concepts. Just got the best of native languages and modern languages. While reading some articles everyone has the same option, Go its very
suited for concurrent applications, since its very easy to use channels, select and goroutines.
For sure, a very good challenger to my usual preference on python

Bad Behavior has blocked 154 access attempts in the last 7 days.

Hyper Smash