Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
becki:linux:golang [2011-06-09 16:15] becki |
becki:linux:golang [2018-02-26 11:24] (aktuell) becki |
||
---|---|---|---|
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 21: | Zeile 22: | ||
Updating Go is described [[golang>doc/install.html?h=weekly+release#releases|here]] | 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 120: | 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 127: | 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 165: | 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 235: | 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 275: | 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 293: | 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 == |