Can someone explain the difference between and and * in GO lang .. and give examples of when and and * will be used to illustrate the difference? From what I read, they both relate to accessing a variable's memory cell, but I'm not sure when to use / /.
Here is a very simple example illustrating the use of &and *. Note that it *can be used for two different things: 1) declare a variable as a pointer 2) to dereference a pointer.
&
*
package main import "fmt" func main() { b := 6 var b_ptr *int // *int is used delcare variable // b_ptr to be a pointer to an int b_ptr = &b // b_ptr is assigned value that is the address // of where variable b is stored // Shorhand for the above two lines is: // b_ptr := &b fmt.Printf("address of b_ptr: %p\n", b_ptr) // We can use *b_ptr get the value that is stored // at address b_ptr, or dereference the pointer fmt.Printf("value stored at b_ptr: %d\n", *b_ptr) }
Result:
address of b_ptr: 0xc82007c1f0 value stored at b_ptr: 6
. " " :
x T &x *T x. [& Hellip;]x *T - *x T, x. x - nil, *x .
x T &x *T x. [& Hellip;]
x
T
&x
*T
x *T - *x T, x. x - nil, *x .
*x
nil
: & ( ) , , * , ( nil, , -).
& - . * , " ".
, p := &SometType{}, , SomeType{}, , , p. p *SomeType, , . * , .
p := &SometType{}
SomeType{}
p
*SomeType
deference, , C ++. , . , p *SomeType, SomeType, someType := *p, .
SomeType
someType := *p
, . , - .
& .
* "" , , .
For types var *type, it means that "* var has a type of type" (and without a variable, it simply means "a pointer to something of type".
var *type
& and * is mainly used in functions:
package main import ( "fmt" ) type Person struct { name string ageYears int areDays int } func main() { john := Person{name: "John", ageYears: 46} toDays(&john) fmt.Println(john) //{John 46 16790} toDays2(john) fmt.Println(john) //{John 46 16790} } func toDays(p *Person) { p.areDays = p.ageYears * 365 } func toDays2(p Person) { p.areDays = -1 }
Source: https://habr.com/ru/post/1612477/More articles:Count individual words in a Pandas data frame - pythonsimplexml does not load tag classes ? - htmlDo we intend to use exception types from the standard library? - c ++Pandas subset Secondary DataFrame index and remapping values - pythonMasked array: how to change a character representing masked values - pythonHow can I get the location and width of the window / window buttons? - javaConfiguring spring boot with hibernate spatial - springCorrectly babel drags a file into ember-cli-build with additional tree - ecmascript-6What is the difference between UIDevice orientation and UIInterface orientation? - iosWhat is the best way to deploy Java AWS Lambda? - javaAll Articles