牛客网Go语言专项训练刷题85题

  1. 对于以下代码,正确的是:a=12
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    package main

    import "fmt"

    func main() {

    a := 12
    {

    a := 13
    _ = a // make compiler happy
    }

    fmt.Println(a)

    }
    本题考察对作用域的了解,在作用域内的 a 在作用域外失效,所以输出 12。要注意 { } 代码块里是 a := 13 是在一个新的作用域内重新声明了一个 a 变量,与外部的 a 变量无关。在 {} 代码块中的变量a为局部变量,只在 {} 中有效!

  1. 比较大众的的说法是go语言的指针不支持运算,所以无法对指针进行自增或自减运算,也不可以对指针进行下标运算。
    Go语言究竟能不能对指下标运算呢?
1
2
3
4
5
6
7
8
9
10
11
12
package main

import "fmt"

func main() {

a := 1024
m := map[string]*int{ "A":&a }
// invalid operation: cannot take address of m["A"] (map index expression of type *int)
fmt.Println(&m["A"])

}

牛客网Go语言专项训练刷题85题

https://blog.jiejaitt.top/posts/75e8d596.html

作者

JIeJaitt

发布于

2021-06-01

更新于

2023-09-16

许可协议

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×