Benutzer-Werkzeuge

Webseiten-Werkzeuge


becki:linux:golang

Dies ist eine alte Version des Dokuments!


Go Programming Language Tips

Setup

Download Go source:

cd /usr/local
hg clone -r release https://go.googlecode.com/hg/ go

Build Toolchain:

cd go/src
./all.bash

Add Go tools to PATH by creating /etc/profile.de/go.sh:

#!/bin/sh
export PATH="$PATH:/usr/local/go/bin"

Go Syntax Highlighting for your editor at http://go-lang.cat-v.org/text-editors/

Hello World

package main
import "fmt"
 
func main() {
    fmt.Printf("Hallo Süße!\n")
}

Build & run: 8g hello.go && 8l -o hello hello.8 && ./hello

For Printf see pkg/fmt/

Variable Declaration

package main
import "fmt"
func main() {
    var i,j int                         // 2 ints, autoinitialized to zero value
    fmt.Printf("%d %d\n", i, j)         // 0 0
 
    var k,l,m,n= true, 0, 2.6, "Hello"  // missing types default to:
    fmt.Printf("%T %T %T %T\n", k,l,m,n)// -> bool int float64 string
    fmt.Printf("%v %v %v %v\n", k,l,m,n)// -> true 0 2.6 Hello
    fmt.Println(k,l,m,n)                // -> true 0 2.6 Hello
 
    o,p:= false, "World"                // short form: no type but initializers:
    fmt.Printf("%T %T\n", o,p)          // -> bool string
    fmt.Println(o,p)                    // -> false World
}

more

Arrays

package main
import "fmt"
func main() {
    //var ia= [...]int{47,11}       // Create an array literal
    ia:= [...]int{47,11}            // Create an array literal - short form
 
    for i, v := range ia {          // Loop through an array
        fmt.Printf("%d %d\n", i, v)
    }
 
    var ib [3]int                   // Create arrays with ZEROED elements
    var ic [2]int
    for _, v := range ib {          // '_' means key not used
        fmt.Printf("%d \n", v)      // -> 0 0 0
    }
 
    //i=0                           // illegal, i not defined outside loop
    //ib = ia                       // illegal, different len => different types
    //ib[3]=9                       // illegal, index out of bounds
    //fmt.Println(ia == ic)         // illegal, no == operator for array
 
    ic= ia                          // copy BY VALUE
    ia[0]= 36
    fmt.Println(ia, ic)             // -> [36 11] [47 11]
 
    //i:=3
    //ib[i]=9                       // panic => runtime index check :-)
}
  • The length of the array is part of its type and cannot grow
  • Arrays are real types, are copied by value, pointers to array are possible

Slices

package main
import "fmt"
func main() {
    a:= [...]int{0,1,2,3}               // an array to play with
    sa:= a[:]                           // Create slice from whole array
    sb:= a[:2]                          // Create slice from last part
    sc:= a[2:]                          // Create slice from first part
    fmt.Println(sa, len(sa), cap(sa))   // -> [0 1 2 3] 4 4
    fmt.Println(sb, len(sb), cap(sb))   // -> [0 1] 2 4(!)
    fmt.Println(sc, len(sc), cap(sc))   // -> [2 3] 2 2
 
    sa[0]=6                             // all slices point to the same array:
    sb[1]=7                             //
    sc[0]=8                             //
    a[3]= 9                             //
    fmt.Println(a, sa, sb, sc)          // -> [6 7 8 9] [6 7 8 9] [6 7] [8 9]
 
    //sb[2]=7                           // panic, although cap is 4!
 
    sb= sa                              // A copy points to the same array:
    fmt.Println(a, sa, sb)              // -> [6 7 8 9] [6 7 8 9] [6 7 8 9]
    sb[0]=0                             //
    fmt.Println(a, sa, sb)              // -> [0 7 8 9] [0 7 8 9] [0 7 8 9]
 
    // fmt.Println(sb==sa)              // invalid, works only with nil
 
    sc= append(sb, 5)                   // append() can create new array:
    fmt.Println(sa, sb, sc)             // -> [0 7 8 9] [0 7 8 9] [0 7 8 9 5]
    sa[1]= 1                            //
    sb[2]= 2                            //
    fmt.Println(sa, sb, sc)             // -> [0 1 2 9] [0 1 2 9] [0 7 8 9 5]
}

more

Cookies helfen bei der Bereitstellung von Inhalten. Diese Website verwendet Cookies. Mit der Nutzung der Website erklären Sie sich damit einverstanden, dass Cookies auf Ihrem Computer gespeichert werden. Außerdem bestätigen Sie, dass Sie unsere Datenschutzerklärung gelesen und verstanden haben. Wenn Sie nicht einverstanden sind, verlassen Sie die Website. Weitere Information
becki/linux/golang.1297864359.txt.gz · Zuletzt geändert: 2011-02-16 13:52 von becki

Impressum - Datenschutzerklärung