Benutzer-Werkzeuge

Webseiten-Werkzeuge


becki:linux:golang

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen Revision Vorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
becki:linux:golang [2011-05-12 13:44]
becki
becki:linux:golang [2018-02-26 11:24] (aktuell)
becki
Zeile 1: Zeile 1:
-====== Go Programming Language Tips ======+====== Go Programming Language Tips ==
 ===== Setup == ===== Setup ==
  
Zeile 14: Zeile 14:
 #!/bin/sh #!/bin/sh
 export PATH="​$PATH:/​usr/​local/​go/​bin"​ export PATH="​$PATH:/​usr/​local/​go/​bin"​
 +export GOROOT=/​usr/​local/​go
 </​code>​ </​code>​
  
Zeile 19: Zeile 20:
  
 Go Syntax Highlighting for your dokuwiki at http://​rosettacode.org/​wiki/​User:​MizardX/​GeSHi_Go.php (Doesn'​t work with 2009-12-25c "​Lemming"​) Go Syntax Highlighting for your dokuwiki at http://​rosettacode.org/​wiki/​User:​MizardX/​GeSHi_Go.php (Doesn'​t work with 2009-12-25c "​Lemming"​)
 +
 +Updating Go is described [[golang>​doc/​install.html?​h=weekly+release#​releases|here]]
 +
 +==== ARM5 (Sheevaplug) specific ==
 +
 +  * Current release version (release.r57.1 / 8295:​95d2ce135523) does not work
 +  * Current weekly version (weekly.2011-06-02 / 8624:​3418f22c39eb) works
 +  * ''​export GOARM=5''​ in ''​~/​.profile''​ !
 +  * export GOHOSTARCH=arm,​ export GOHOSTOS=linux,​ export GOARCH=arm, export GOOS=linux may be necessary too
 +
 +==== Build Crosscompiler on x86 for ARM5 ==
 +
 +Export the following variables before running ''​src/​all.bash'':​
 +
 +<code bash>
 +export GOROOT=$(pwd)
 +export GOHOSTARCH=386
 +export GOHOSTOS=linux
 +export GOARCH=arm
 +export GOOS=linux
 +export GOARM=5
 +</​code>​
 +
  
 ===== Hello World == ===== Hello World ==
Zeile 31: Zeile 55:
 </​code>​ </​code>​
  
-Build & run: ''​8g hello.go && 8l -o hello hello.8 && ./​hello''​+Build & run:  
 +x86: ''​8g hello.go && 8l -o hello hello.8 ​&& ./​hello''​ 
 + 
 +arm: ''​5g hello.go && 5l -o hello hello.5 ​&& ./​hello''​
  
 For ''​Printf''​ see [[golang>​pkg/​fmt/​]] For ''​Printf''​ see [[golang>​pkg/​fmt/​]]
Zeile 115: Zeile 142:
   * Arrays are copied by value :!:   * Arrays are copied by value :!:
   * A Pointer to an array is possible (unlike in C where the pointer represents the array)   * A Pointer to an array is possible (unlike in C where the pointer represents the array)
 +
 </​note>​ </​note>​
  
Zeile 122: Zeile 150:
 s := []int{0, 1, 2, 3}                  // a slice created literally s := []int{0, 1, 2, 3}                  // a slice created literally
 s := make([]int, 4)                     // create a zerored slice s := make([]int, 4)                     // create a zerored slice
 +len(s) // get number of items in slice
 +cap(s) // get actual slice capacity
 </​code>​ </​code>​
  
Zeile 160: Zeile 190:
  
 <note tip> <note tip>
- 
   * Slices are copied //by value// but the internal arrays are copied //by reference// :!:   * Slices are copied //by value// but the internal arrays are copied //by reference// :!:
   * Slices have a length (number of items) and a capacity (length of underlying array(?))   * Slices have a length (number of items) and a capacity (length of underlying array(?))
 +
 </​note>​ </​note>​
 <note important>​Appending to a Slice results in a new slice. The new slice may point to a different array than the original slice.</​note>​ <note important>​Appending to a Slice results in a new slice. The new slice may point to a different array than the original slice.</​note>​
Zeile 230: Zeile 260:
   * In order to really work on the object, the receiver of the method must be a //pointer// to the object, otherwise the method operates ony on an (anonymous) copy.   * In order to really work on the object, the receiver of the method must be a //pointer// to the object, otherwise the method operates ony on an (anonymous) copy.
   * Invoking methods on //pointers to objects// has the same syntax and work the same as invoking the method directly on the object.   * Invoking methods on //pointers to objects// has the same syntax and work the same as invoking the method directly on the object.
 +
 </​note>​ </​note>​
  
Zeile 270: Zeile 301:
  
     o= Point{} ​                         // reset to {0 0}     o= Point{} ​                         // reset to {0 0}
-    isv= &​o ​                            // isv now is a POINTER to o:+    isv= &​o ​                            // isv now is a POINTER(!) to o:
     fmt.Printf("​%T\n",​ isv)             // -> *main.Point     fmt.Printf("​%T\n",​ isv)             // -> *main.Point
     o.x= 9                              // updates to object are seen by isv     o.x= 9                              // updates to object are seen by isv
Zeile 288: Zeile 319:
  
 <note tip> <note tip>
 +An Interface can store any value that implemts it. This can be a value //or// a pointer to a value.
 +
 The only way to directly operate on an object via an interface is to The only way to directly operate on an object via an interface is to
-  * implement ​the methods of the interface with an object //pointer// as receiver +  * Implement ​the methods of the interface with an object //pointer// as receiver 
-  * instantiate ​the interface with the //adress// of the object+  * Instantiate ​the interface with the //adress// of the object 
 </​note>​ </​note>​
 +<note important>​Some :?: library functions which return an interface in reality return a pointer to an implementation of the interface (see e.g. [[golang>​pkg/​net/#​Listener.Listen|net.Listen]])</​note>​
  
-===== Error Handling ==+===== Handling ​Errors ​==
  
-Defer, Panic, Recover: http://​blog.golang.org/​2010_08_01_archive.html ​=> The convention in the Go libraries is that even when a package uses panic internally, its external API still presents ​explicit ​error return ​values.+Defer, Panic, Recover: http://​blog.golang.org/​2010_08_01_archive.html ​⇒ The convention in the Go libraries is that even when a package uses panic internally, its external API still returns ​explicit ​''​os.Error'' ​values. 
 + 
 +[[golang>​pkg/​os/#​Error|os.Error]] is the same interface as [[golang>​pkg/​fmt/#​Stringer|fmt.Stringer]],​ i.e. it has a method called ''​String()''​ wich returns a ''​string''​. Thus an instance of os.Error can always be passed to the functions in ''​fmt''​ and ''​log''​ directly, without explicitely calling the ''​String()''​ method. E.g: 
 + 
 +<code go> 
 +if err != nil { 
 +    log.Panic(err) 
 +
 +</​code>​ 
 + 
 +FIXME See [[:​becki:​my:​linux:​exception_handling_in_c]]
  
 ===== Unsorted Things == ===== Unsorted Things ==
- + 
 +  * Since [[golang>​doc/​go_tutorial.html#​tmp_94|strings are immutable values]] I guess only references are passed around if you pass the type ''​string''​. Thus it probably does not make much sense to use pointers to strings.
   * Seems to be convention that a function returns (among others) ''​os.Error == nil''​ when it succeded (tested)   * Seems to be convention that a function returns (among others) ''​os.Error == nil''​ when it succeded (tested)
   * ''​if''​ and ''​switch''​ accept an initialization statement, see [[golang>​doc/​effective_go.html#​if]]   * ''​if''​ and ''​switch''​ accept an initialization statement, see [[golang>​doc/​effective_go.html#​if]]
   * Number <=> String conversion is done with [[golang>​pkg/​strconv/​]]   * Number <=> String conversion is done with [[golang>​pkg/​strconv/​]]
 +  * [[http://​www.syntax-k.de/​projekte/​go-review|Interesting Go overview]]
  
 ===== Todo == ===== Todo ==
becki/linux/golang.1305207896.txt.gz · Zuletzt geändert: 2011-05-12 13:44 von becki

Impressum - Datenschutzerklärung