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-16 13:45] becki |
becki:linux:golang [2018-02-26 11:24] (aktuell) becki |
||
|---|---|---|---|
| Zeile 30: | Zeile 30: | ||
| * export GOHOSTARCH=arm, export GOHOSTOS=linux, export GOARCH=arm, export GOOS=linux may be necessary too | * export GOHOSTARCH=arm, export GOHOSTOS=linux, export GOARCH=arm, export GOOS=linux may be necessary too | ||
| - | ==== Crosscompiling on x86 for ARM5 == | + | ==== Build Crosscompiler on x86 for ARM5 == |
| + | |||
| + | Export the following variables before running ''src/all.bash'': | ||
| <code bash> | <code bash> | ||
| + | export GOROOT=$(pwd) | ||
| export GOHOSTARCH=386 | export GOHOSTARCH=386 | ||
| export GOHOSTOS=linux | export GOHOSTOS=linux | ||
| Zeile 39: | Zeile 42: | ||
| export GOARM=5 | export GOARM=5 | ||
| </code> | </code> | ||
| + | |||
| ===== Hello World == | ===== Hello World == | ||
| Zeile 138: | 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 145: | 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 183: | 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 253: | 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 293: | 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 311: | 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> | ||
| ===== Handling Errors == | ===== 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 returns explicit ''os.Error'' 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: | [[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: | ||
| Zeile 332: | Zeile 344: | ||
| ===== 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]] | ||