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)
|
||||
}
|
||||
Reference in New Issue
Block a user