当前位置:网站首页>Fmt Must the result of println (true) be true?

Fmt Must the result of println (true) be true?

2022-06-26 06:44:00 Coding advanced

background

Honeycomb Chief development evangelist Jessica stay Twitter A status message was posted on :

fmt.Println("What is truth?", true)

can output:

What is truth? false

The execution result of the following code may be What is truth? false

fmt.Println("What is truth?", true)

You can first think about the circumstances under which such a result will occur .

analysis

Let's look at the following code :

// identifier.go
package main

import "fmt"

func main() {
    true := false
    fmt.Println(true)
}

We think this code will compile and report errors , Or normal execution ?

The actual execution result is to print false, It will not compile and report errors .

So the code at the beginning of this article fmt.Println("What is truth?", true) It is possible to print What is truth? false Of .

Some students may be curious ? Why is this so ?true No Go Keywords of language , Why can an identifier also be defined as true The variable of ?

The answer is :true Not at all Go Keywords of language ,Go Language is currently only 25 Key words , As shown below :

break        default      func         interface    select
case         defer        go           map          struct
chan         else         goto         package      switch
const        fallthrough  if           range        type
continue     for          import       return       var

these keyword Can't be used as Go Identifier of language .true yes Pre declaration identifier , It can be used as Go Identifier of language , The official instructions are as follows :

Predeclared identifiers

The following identifiers are implicitly declared in the universe block:

Types:
    bool byte complex64 complex128 error float32 float64
    int int8 int16 int32 int64 rune string
    uint uint8 uint16 uint32 uint64 uintptr

Constants:
    true false iota

Zero value:
    nil

Functions:
    append cap close complex copy delete imag len
    make new panic print println real recover

therefore true := false Such code is in Go The language can be compiled normally , also go vet No potential errors will be detected .

not only true, All identifiers in the pre declaration identifier can be used as identifiers of global variables and local variables , Consider the following code :

// identifier2.go
package main

import "fmt"

var nil = 100

var false = true

func main() {
    true := false
    fmt.Println(true, false, nil)
}

You can think about the output ?

  • A: true false nil
  • B: true false 100
  • C: true true 100
  • D: false true 100
  • E: false true nil

Those who want to know the answer can send messages to the official account. bsf Get answers .

summary

Go This feature of language has also attracted a lot of attention controversy , just as Go The error handling is the same .

We as users , We need to pay attention to :Go Keywords are not allowed as identifiers , Others can be used as identifiers .

Open source address

Articles and sample code are open source GitHub: Go Primary language 、 Intermediate and advanced tutorials .

official account :coding Advanced . Pay attention to the official account and get the latest information. Go Interview questions and technical stack .

Personal website :Jincheng's Blog.

You know : No taboo .

References

原网站

版权声明
本文为[Coding advanced]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202171319036184.html