“Pointer di Golang” Kode Jawaban

Pergi pointer

p := Vertex{1, 2}  // p is a Vertex
q := &p            // q is a pointer to a Vertex
r := &Vertex{1, 2} // r is also a pointer to a Vertex

// The type of a pointer to a Vertex is *Vertex

var s *Vertex = new(Vertex) // new creates a pointer to a new struct instance
DevLorenzo

Pergi pointer

&	address of / create pointer
*	dereference pointer
DevLorenzo

Pergi pointer

func main() {
	i, j := 42, 2701

	p := &i         // point to i
	fmt.Println(*p) // read i through the pointer
	*p = 21         // set i through the pointer
	fmt.Println(i)  // see the new value of i

	p = &j         // point to j
	*p = *p / 37   // divide j through the pointer
	fmt.Println(j) // see the new value of j
}
DevLorenzo

Pointer di Golang

func main () {
  b := *getPointer()
  fmt.Println("Value is", b)
}
Harendra

Pointer Golang

package main

import "fmt"

func main() {
	// create a normal string variable
	name := "original"
	// pass in a pointer to the string variable using '&'
	setName(&name, "boot.dev")
	fmt.Println(name)
}

func setName(ptr *string, newName string) {
	// dereference the pointer so we can modify the value
	// and set the value to "boot.dev"
	*ptr = newName
}
JHAN89

Jawaban yang mirip dengan “Pointer di Golang”

Pertanyaan yang mirip dengan “Pointer di Golang”

Lebih banyak jawaban terkait untuk “Pointer di Golang” di Go

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya