I. How to find this problem?

I was looking through Twitter when BLOGGER David Crawshaw shared a piece of code

Twitter address: twitter.com/davidcrawsh…

The original post code is as follows, click to run in Play:

package main import "fmt" func main() { if a := 1; false { } else if b := 2; false { } else if c := 3; False {} else {fmt.Println(a, b, c) // result: 1, 2, 3}}Copy the code

So, extend it as follows:

package main import "fmt" func main() { if a := 1; false { aa := 11 } else if b := 2; false { bb := 22 } else if c := 3; False {cc := 33} else {fmt.Println(a, b, c) fmt.Println(aa, bb, cc)}Copy the code

Try this again:

package main import "fmt" func main() { if x := 10; X < 9 {fmt.println (x)} else {fmt.println ("not x")} fmt.println (x) //Copy the code

A little more imagination:

package main

import "fmt"

func main() {
	if a := 1; false {
		fmt.Println(a, b, c) // undefined: b, c
	} else if b := 2; false {
		fmt.Println(a, b, c) // undefined: c
	} else if c := 3; false {
		fmt.Println(a, b, c)
	} else {
		fmt.Println(a, b, c)
	}
}
Copy the code

A reasonable guess:

package main

import "fmt"

func main() {
	if a := 1; false {

	} else if b := 2; false {

	} else if c := 3; false {

	} else {
		if a := 11; true {
			fmt.Println(a, b, c) // 11 2 3
		}
		fmt.Println(a, b, c) // 1 2 3
	}
}
Copy the code

So a preliminary conclusion can be drawn:

Only the variable declaration scope in the if/else-if conditional expression goes down to the endelseInternally, it is obvious that it cannot be applied upward, and the scope of scope is limited to this level, which does not affect the normal variable scope and shielding effect.

What are the advantages and disadvantages of doing so?

A. benefits:

  1. Such as the followingChris Hines Comment on the listed codeWhen processing SQL tasks, the return value and error processing, logically, relatively smooth.
if result, err := db.Exec(updateSql, ...) ; err ! = nil { return err } else if count, err := result.RowsAffected(); err ! = nil { return err } else if count ! = 1 { return ErrNotUpdated }Copy the code
  1. Another comment code, emMMmm, how to say? The code looks cleaner and more elegant this way, but it can be tricky to read for those unfamiliar with the feature.
if got, err := f(); err ! = nil {// barb barb barb} else if want :=... ; got ! = want { t.Errorf(... , got, want) }Copy the code

Fortunately, we can add comments. Change to the following for better readability:

// NOTE: The scope of the global offset table runs through the entire if-else... if got, err := f(); err ! = nil {// barb barb barb} else if want :=... ; got ! = want { t.Errorf(... , got, want) }Copy the code
  1. This is how the language is designed: refer to the design documentation: golang.org/ref/spec#If… There is no official benefit to doing so. But it does make writing code easier.

B. the downside:

  1. Emmmmm is a strange feature for people who don’t know it. Not very well understood, not in line with the core principle of programming: code is for people first, and for machines to run.