头图

background

Honeycomb 's lead developer evangelist Jessica a status:

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

can output:

What is truth? false

It means that 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 would occur.

Parse

Let's look at the following code:

// identifier.go
package main

import "fmt"

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

Do you think this code will compile and report an error, or will it execute normally?

The actual execution result is to print false , and it will not compile and report an error.

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

Some students may be curious? Why is this so? Isn't true a keyword in Go language? Why can you define a variable with an identifier of true ?

The answer is: true is not a keyword of Go language, Go language currently has only 25 keywords, as follows:

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 keywords cannot be used as identifiers in Go language. true is the pre-declared identifier , which can be used as the identifier of the Go language. The official description is 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, code like true := false can be compiled normally in Go, and go vet will not detect any potential errors.

Not only true , but all identifiers in predeclared identifiers can be used as identifiers for global and local variables, such as the following code:

// identifier2.go
package main

import "fmt"

var nil = 100

var false = true

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

Can you imagine what the output is?

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

If you want to know the answer, you can send a message to the official account at bsf to get the answer.

Summarize

This feature of the Go language has also caused a lot , as has 's error handling.

As a user, we need to pay attention: Go does not allow keywords as identifiers, others can be used as identifiers .

open source address

The article and sample code are open sourced on GitHub: Go language beginner, intermediate and advanced tutorial .

Official account: coding advanced. Follow the official account to get the latest Go interview questions and technology stacks.

Personal website: Jincheng's Blog .

Know: Wuji .

References


coding进阶
116 声望18 粉丝