feat: pointers, runes, structs
This commit is contained in:
26
go-by-example/pointers/pointers.go
Normal file
26
go-by-example/pointers/pointers.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// This functions creates a copy of the argument and does not change the original
|
||||
func zeroval(ival int) {
|
||||
ival = 0
|
||||
}
|
||||
|
||||
// Changes the value of the argument because it is a pointer.
|
||||
func zeroptr(iptr *int) {
|
||||
*iptr = 0
|
||||
}
|
||||
|
||||
func main() {
|
||||
i := 1
|
||||
fmt.Println("initial", i)
|
||||
|
||||
zeroval(i)
|
||||
fmt.Println("zeroval", i)
|
||||
// & operand means the address of the variable. Creates a pointer when a function needs that as argument
|
||||
zeroptr(&i)
|
||||
fmt.Println("zeroptr:", i)
|
||||
|
||||
fmt.Println("Poiner:", &i)
|
||||
}
|
||||
40
go-by-example/strings-runes/strings-runes.go
Normal file
40
go-by-example/strings-runes/strings-runes.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func main() {
|
||||
const s = "สวัสดี"
|
||||
// Will output thte byte length and not the string length.
|
||||
fmt.Println("Len:", len(s))
|
||||
|
||||
for i := range len(s) {
|
||||
fmt.Printf("%x ", s[i])
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
fmt.Println("Rune Count: ", utf8.RuneCountInString(s))
|
||||
|
||||
for idx, runeValue := range s {
|
||||
fmt.Printf("%#U starts at %d\n", runeValue, idx)
|
||||
}
|
||||
|
||||
fmt.Println("\nUsing DecodeRuneInString")
|
||||
for i, w := 0, 0; i < len(s); i += w {
|
||||
runeValue, width := utf8.DecodeRuneInString(s[i:])
|
||||
fmt.Printf("%#U starts at %d\n", runeValue, i)
|
||||
w = width
|
||||
|
||||
examineRune(runeValue)
|
||||
}
|
||||
}
|
||||
|
||||
func examineRune(r rune) {
|
||||
if r == 't' {
|
||||
fmt.Println("found tee")
|
||||
} else if r == 'ส' {
|
||||
fmt.Println("found so sua")
|
||||
}
|
||||
}
|
||||
42
go-by-example/structs/structs.go
Normal file
42
go-by-example/structs/structs.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type person struct {
|
||||
name string
|
||||
age int
|
||||
}
|
||||
|
||||
func newPerson(name string) *person {
|
||||
p := person{name: name}
|
||||
p.age = 42
|
||||
return &p
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(person{"Bob", 20})
|
||||
fmt.Println(person{"Alice", 30})
|
||||
// fmt.Println(person{"Fred"}) // will zero value age. Compiler don't like it actually.
|
||||
fmt.Println(&person{"Ann", 40})
|
||||
fmt.Println(newPerson("Jon"))
|
||||
|
||||
s := person{name: "Sean", age: 50}
|
||||
|
||||
fmt.Println(s.name)
|
||||
|
||||
sp := &s
|
||||
fmt.Println(sp.age)
|
||||
|
||||
sp.age = 51
|
||||
fmt.Println(sp.age)
|
||||
|
||||
// If a single use struct you can omit the decalre the struct type and just use it directly on the initialization.
|
||||
dog := struct {
|
||||
name string
|
||||
isGood bool
|
||||
}{
|
||||
"Mike",
|
||||
true,
|
||||
}
|
||||
fmt.Println(dog)
|
||||
}
|
||||
Reference in New Issue
Block a user