[golang] learning note and resources

Primers

Semantic Statments

Notes

keywords

    package
    import
    func
    type
    interface
    var
    const
    range
    switch
    case

Concurrency and goroutine [TBC]

Printf format [TBC]

methods and interfaces

func ScaleFunc(v *Vertex, f float64) {
  (*v).X = (*v).X * f
  v.Y = v.Y * f
}

Interface

moretypes

Variadic functions [TBC]

Closures

Functions

Maps

init value:

make(map[key_type]value_type)

x = map[key_type]value_type{};

delete(map,key)

map[key]

Arrays and Slice

Exercises

range, a literal for for-loop

package main

import "golang.org/x/tour/pic"
import "fmt";
func Pic(dx, dy int) (out[][]uint8) {
out = make([][]uint8, dx)
xs := make([]int, dx);
ys := make([]int, dy);
for i,_ := range xs{
  out[i] = make([]uint8, dy)
  for j,_:=range ys{
    out[i][j] = (uint8)(i+j);
  }
  }
  fmt.Println("Called","Pic",dx,dy);
return;
}

func main() {
	pic.Show(Pic) // [IMAGE not showing correctly]
}

getiem

package main

import "fmt"

func main() {
	var s []int;
	a := []int{2, 3, 5, 7, 11, 13}
	s = a;
	s = a[1:4]
	fmt.Println(s)

	s = a[:2]
	fmt.Println(s)

	s = a[1:]
	fmt.Println(s)

    s = a[1:len(a)]
	fmt.Println(s)

	s = a[1 : len(a)-1]
	fmt.Println(s)
}

Structure literal

type Vertex struct {
	X, Y int
}
var v1 Vertex   =  Vertex{X: 1, Y: 2}
var v2 int      =  int( 5 );

var v3          =  Vertex{X: 1, Y: 2}
var v4          =  int( 5 );

// var v5       =  {X: 1, Y: 2} /// gives an error!
var v6          =  5;

exercise-slices.go

package main

import "golang.org/x/tour/pic"
import "fmt";
func Pic(dx, dy int) (out[][]uint8) {
out = make([][]uint8, dx)
xs := make([]int, dx);
ys := make([]int, dy);
for i,_ := range xs{
  out[i] = make([]uint8, dy)
  for j,_:=range ys{
    out[i][j] = (uint8)(i+j);
  }
  }
  fmt.Println("Called","Pic",dx,dy);
return;
}

func main() {
  pic.Show(Pic)
}

exercise-maps.go

package main

import (
  "golang.org/x/tour/wc"
//  "fmt"
  "strings"
)

func WordCount(s string) map[string]int {
  out := map[string]int{};
  for i,w :=  range strings.Fields(s){
    _ = i
  out[w] +=1;
  }
  return out
}

func main() {
  wc.Test(WordCount)
}

exercise-stringer.go

package main

import "fmt"

type IPAddr [4]byte

// TODO: Add a "String() string" method to IPAddr.

func main() {
  hosts := map[string]IPAddr{
    "loopback":  {127, 0, 0, 1},
    "googleDNS": {8, 8, 8, 8},
  }
  for name, ip := range hosts {
    fmt.Printf("%v: %v\n", name, ip)
  }
}

exercise-images.go

package main

import "golang.org/x/tour/pic"
import "image"
import "image/color"

//type Image image.Image
type Image struct{
minX int
maxX int
minY int
maxY int
}

func (x *Image) Bounds() (image.Rectangle){

return image.Rect( x.minX, x.minY, x.maxX,x.maxY)
}

func (x *Image) ColorModel() (color.Model){
return color.RGBAModel
}

func (im *Image) At(x int, y int) color.Color{
pic := Pic( im.maxX, im.maxY)
return color.RGBA{pic[x][y],pic[x][y], 255, 255}
}

func Pic(dx, dy int) (out[][]uint8) {
out = make([][]uint8, dx)
xs := make([]int, dx);
ys := make([]int, dy);
for i,_ := range xs{
  out[i] = make([]uint8, dy)
  for j,_:=range ys{
    out[i][j] = (uint8)(i+j);
  }
  }
//  fmt.Println("Called","Pic",dx,dy);
return out;
}

func main() {
  m := Image{0,200,0,200}
  pic.ShowImage(&m)
}
package main

import "golang.org/x/tour/pic"
import "image"
import "image/color"

//type Image image.Image
type Image struct{
minX int
maxX int
minY int
maxY int
}

func (x *Image) Bounds() (image.Rectangle){

return image.Rect( x.minX, x.minY, x.maxX,x.maxY)
}

func (x *Image) ColorModel() (color.Model){
return color.RGBAModel
}

func (im *Image) At(x int, y int) color.Color{
pic := Pic( im.maxX, im.maxY)
return color.RGBA{pic[x][y],pic[x][y], 255, 255}
}

func Pic(dx, dy int) (out[][]uint8) {
out = make([][]uint8, dx)
xs := make([]int, dx);
ys := make([]int, dy);
for i,_ := range xs{
  out[i] = make([]uint8, dy)
  for j,_:=range ys{
    out[i][j] = (uint8)(i+j);
  }
  }
//  fmt.Println("Called","Pic",dx,dy);
return out;
}

func main() {
  m := Image{0,200,0,200}
  pic.ShowImage(&m)
}

exercise-rot-reader.go

package main

import (
  "io"
  "os"
  "strings"
  "fmt"
)

type rot13Reader struct {
  r io.Reader
}

func (r rot13Reader) Read (bts []byte) (int, error){
    intVal,e :=r.r.Read(bts)
  //fmt.Println(bts);
  for i:=0;i<intVal;i++{
     x:=bts[i]
     var y byte;
     if (x>=65 && x<97){
       y = (x - 65 + 13)%26 + 65
     }else if(x >= 97){
       y = (x -97 + 13)%26 + 97     
     }else{
       y = x+13;
       fmt.Println(x,y);
     }
     bts[i] = y
  }
  
  return intVal,e
}

func main() {
  s := strings.NewReader("Lbh penpxrq gur pbqr!")
  r := rot13Reader{s}
    io.Copy(os.Stdout, &r)
  return;
//var buf []byte;
  buf := make([]byte,100);
  for {
   i,e := r.Read(buf)
   _ = e
   _ = i 
   fmt.Printf("%s\n",buf)
   if e==io.EOF{
       break
   }
  }
  return 
    io.Copy(os.Stdout, &r)
}

Dependency management

Summary

Grammar

Next Topics

Vocabularies. FileIO, strings, OrderedDict, Json, Yaml, Html, Regex

Packaging and Testing

Profiling and Concurrency

Debugging and Tracing

Comments