On the Golang pointer

Source: Internet
Author: User

The first contact with Golang language, the understanding of Go is temporarily more limited.
The go pointer is simply learned here, and it feels similar to the C pointer you learned before.

The address character of the Go language is &, and the memory address of the corresponding variable is returned by using it before a variable is placed.
adding * to the address is the operation to take the corresponding value of the address.

The practice code is as follows:

package mainimport "fmt"func main() {    var a int = 10    var b string = "hello world"    var c bool = true    var d float32 = 0.002    var e float64 = 0.00000003    fmt.Printf("a变量的地址: %x,a的值为:%d\n", &a,*&a)    fmt.Printf("变量的地址: %x\n", &b)    fmt.Printf("变量的地址: %x\n", &c)    fmt.Printf("变量的地址: %x\n", &d)    fmt.Printf("变量的地址: %x\n", &e)    var x int = 100    var y int = 10    swap(&x,&y)    fmt.Printf("x=%d,y=%d",x,y)} /*利用指针交互xy值 */func swap(x *int, y *int) {    var temp int    temp = *x    /* 保存 x 地址的值 */    *x = *y      /* 将 y 赋值给 x */    *y = temp    /* 将 temp 赋值给 y */ }

Log:


1530808982516.jpg

At present, the understanding of Go language is very limited, I hope I can also step by step learning to accumulate into the lakes and rivers in the great god it! Ha ha

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.