zoukankan      html  css  js  c++  java
  • 66_Go基础_1_33 指针

     1 package main
     2 
     3 import "fmt"
     4 
     5 func main() {
     6     /*
     7         指针:pointer
     8             存储了另一个变量的内存地址的变量。
     9 
    10     */
    11 
    12     // 1.定义一个int类型的变量
    13     a := 10
    14     fmt.Println("a的数值是:", a)     // 10
    15     fmt.Printf("%T\n", a)        // int
    16     fmt.Printf("a的地址是:%p\n", &a) //0xc0000140a8
    17 
    18     // 2.创建一个指针变量,用于存储变量a的地址
    19     var p1 *int
    20     fmt.Println(p1) // <nil>,空指针
    21     p1 = &a
    22     fmt.Println("p1的数值:", p1)       // p1的数值: 0xc0000140a8
    23     fmt.Printf("p1自己的地址:%p\n", &p1) // p1自己的地址:0xc000006030
    24     fmt.Println("p1的数值,是a的地址,该地址存储的数据:", *p1)
    25     // p1的数值,是a的地址,该地址存储的数据: 10
    26 
    27     // 3.操作变量,更改数值 ,并不会改变地址
    28     a = 100
    29     fmt.Println(a)
    30     fmt.Printf("%p\n", &a) // 0xc0000140a8
    31 
    32     // 4.通过指针,改变变量的数值
    33     *p1 = 200
    34     fmt.Println(a) // 200
    35 
    36     // 5.指针的指针
    37     var p2 **int
    38     fmt.Println(p2)                     // <nil>
    39     p2 = &p1                            //
    40     fmt.Printf("%T,%T,%T\n", a, p1, p2) // int, *int, **int
    41     fmt.Println("p2的数值:", p2)           // p2的数值: 0xc000006030  p1的地址
    42     fmt.Printf("p2自己的地址:%p\n", &p2)     // p2自己的地址:0xc000006038
    43     fmt.Println("p2中存储的地址,对应的数值,就是p1的地址,对应的数据:", *p2)
    44     // p2中存储的地址,对应的数值,就是p1的地址,对应的数据: 0xc0000140a8
    45     fmt.Println("p2中存储的地址,对应的数值,再获取对应的数值:", **p2)
    46     // p2中存储的地址,对应的数值,再获取对应的数值: 200
    47 
    48 }
  • 相关阅读:
    ssm之spring+springmvc+mybatis整合初探
    mybatis缓存之整合第三方缓存工具ehcache
    mybatis缓存之二级缓存
    mybatis缓存之一级缓存
    mybatis动态sql之利用sql标签抽取可重用的sql片段
    mybatis动态sql之bind标签
    mybatis动态sql之内置参数_parameter和_databaseId
    mybatis动态sql之使用foreach进行批量插入的两种方式
    mybatis动态sql之foreach补充(三)
    Visitor Pattern
  • 原文地址:https://www.cnblogs.com/luwei0915/p/15629941.html
Copyright © 2011-2022 走看看