August 2019
Beginner to intermediate
798 pages
17h 2m
English
In this subsection, you will learn more things about the unsafe package and its capabilities with the help of another small Go program named moreUnsafe.go. This will be presented in three parts. What moreUnsafe.go does is access all the elements of an array using pointers.
The first part of the program is next:
package main import ( "fmt" "unsafe" )
The second part of moreUnsafe.go comes with the next Go code:
func main() {
array := [...]int{0, 1, -2, 3, 4}
pointer := &array[0]
fmt.Print(*pointer, " ")
memoryAddress := uintptr(unsafe.Pointer(pointer)) + unsafe.Sizeof(array[0]) for i := 0; i < len(array)-1; i++ { pointer = (*int)(unsafe.Pointer(memoryAddress)) fmt.Print(*pointer, " ") memoryAddress = ...Read now
Unlock full access