课程: Practice It: Go REST API Server

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Pointers vs. value methods

Pointers vs. value methods

- [Instructor] Let's step back and talk about Pointers, which Go does have. A pointer holds memory address of a variable's value. For instance, type '*t' is a pointer to a t variable value, and currently it's value is nil. Let's look at an example. First, we'll declare two int variables, x and y. And as you can see, we're using the shorthand assignment. So x will be five, and y will be 10. Next, we'll set the pointer n to the address of x, which we will do by using the ampersand, where the asterisk points to the pointer's underlying value. So this will be = &x, then we'll print out n. In this case, the asterisks will be pointing to the underlying value. And if we want to change the value of n, we do it like this. Star, *n = 50, and we print out x. And we use the plain equal sign, because we are setting the value, not instantiating a variable, and setting the value. And if we print out the value of x, we can see that…

内容