The Game of Go

I went to give a try in Go language.

Please consider this before starting something in Go :
– this can be seen as a recession if you are used to developing in Groovy, Scala or Python or if you need to have a comfortable, flexible syntax
– this can be seen as cheating if you use C or Pascal and if you are used to very strict snippets shapes (signature first, then declaration of vars, then code)

Here are the things similar and the things unfamiliar with Go :
– it uses pointers (Goodbye java lovers). Everything not referenced in pointers will be lost if the the current element of stackTrace is dismissed.
– it is not (so much) typed. There are different ways to declare a var. Here are two examples :
* var foo string //(note that the type appears AFTER the var name)
* bar := « test » //(note that it uses ‘:=’ as « this is a new var », else it would be ‘=’)
– it has some structs instead of objects. But you can declare some methods linked to the objects :

struct SayHelloToMe {
Name string //(the convention names looks like C# rather than C or Java, and there is no semicolon here)
}

func (SayHello SayHelloToMe) do () string { //func keyword, then owner struct, then function name, then returned object type
fmt.Println("Hello " + SayHello.Name)
}

Me := SayHelloToMe {Name:"Joe"} //No constructor necessary, the fields are initialized under brackets instead
Me.do() //Here, the func is called

– it can import a lot of different libraries. Just say import « fmt » at the begining of the code and use the fmt object as if it always existed.
– it uses a different notation for the loops. There is no « while » keyword there, only « for » exists :

for toto < 5 {...}
for key,element := range mySlice {...} //a slice is an array in the Go jargon.
for _,element := range mySlice {...} //To get only the element

– the pointers work as in C, except they are used with « new » or « make » instead of malloc :

newTree := new(Tree) // Tree is a struct here
make([]*Tree,10) //new array of 10 pointers, each one of a Tree
newTree = &Tree {"parent", nil, nil} //save the address of the new object in a pointer var.

– you can cast an object as you would do it in python : obj := string(myObj)
– one big project is written in Go : « docker », a system of software image building (infrastucture as code). The binaries are really lightweight (it does not produce bytecode, but executable files, the source must be compiled when switching of platform)

Here is an example of code made to resolve a challenge on a very popular platform of code challenges :
The principle of this code is to know how much nodes are needed in a tree to store several telephone numbers (what is the minimum numbers of digits needed to store a list of telephone numbers).
Here is the solution below, in Go (you can use it for your own needs if you find something interesting inside) :


package main

import "fmt"

type Tree struct {
Value uint8 //uint8 = char
Children []* Tree
}

func countNodes (aTree Tree) int {
if aTree.Children == nil {
return 1
}
sum := 1
for _,element := range aTree.Children {
sum += countNodes (*element)
}
return sum
}

func addTelephone (aTree* Tree, telephone string) {
var thisNode *Tree
telephoneSuffix := telephone
thisNode = aTree
for len(telephoneSuffix) > 0 { //len can be strlen or length of an array
if (*thisNode).Children == nil {
(*thisNode).Children = make([]*Tree,0)
}
var foundChild * Tree
for _,element := range (*thisNode).Children {
if element.Value == telephoneSuffix [0] {
foundChild = element
}
}
if (foundChild != nil) {
thisNode = foundChild
telephoneSuffix = telephoneSuffix[1:len(telephoneSuffix)] //substring or subarray
continue
}

newTree := new(Tree)
newTree.Value = telephoneSuffix[0]

newChildren := append ((*thisNode).Children, newTree)
(*thisNode).Children=newChildren

thisNode = newTree
telephoneSuffix = telephoneSuffix[1:len(telephoneSuffix)]
}
}

/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/

func main() {
var N int
var TheTree *Tree
fmt.Scan(&N)

TheTree = &Tree{0, nil}
for i := 0; i < N; i++ {
var telephone string
fmt.Scan(&telephone)
addTelephone(TheTree, telephone)
}

count := countNodes (*TheTree) - 1

fmt.Println(count) // The number of elements (referencing a number) stored in the structure.
}

Of course the solution is simple, but we have to get used to the syntax. Off you GO then.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

*