2019-07-16 := 的由来和 range 遍历

:= 的由来

0.23 BNF 和 EBNF，非终结符和终结符，开始符号及产生式

-> 定义为（也可用 := 和 ::= 表示）

The symbol is called “becomes” and was introduced with IAL (later called Algol 58) and Algol 60. It is the symbol for assigning a value to a variable. One reads x := y; as “x becomes y”.

Using “:=” rather than “=” for assignment is mathematical fastidiousness; to such a viewpoint, “x = x + 1” is nonsensical. Other contemporary languages might have used a left arrow for assignment, but that was not common (as a single character) in many character sets.

Algol 68 further distinguished identification and assignment; INT the answer = 42; says that “the answer” is declared identically equal to 42 (i.e., is a constant value). In INT the answer := 42; “the answer” is declared as a variable and is initially assigned the value 42.

There are other assigning symbols, like +:=, pronounced plus-and-becomes; x +:= y adds y to the current value of x, storing the result in x.

(Spaces have no significance, so can be inserted “into” identifiers rather than having to mess with underscores)

range 删除元素

func main() {
// 删除切片中的某个元素

// 删除切片中指定元素
{
items := []int{1, 2, 4, 2, 3, 0}
deleteItem := 2
for i := 0; i < len(items); i++ {
if items[i] == deleteItem {
items = append(items[:i], items[i+1:]...)
i--
}
}
fmt.Println(items) // output: [1, 4, 3, 0]
}

// 删除找到的第一个元素
{
// 切片比较大的话，还是用普通的 for 循环比较好
items := []int{1, 2, 4, 2, 3, 0}
deleteItem := 2
for i, item := range items {
// 找到要删除的第一个元素，删除并退出循环
if item == deleteItem {
items = append(items[:i], items[i+1:]...)
break
}
}
fmt.Println(items) // output: [1, 4, 2, 3, 0]
}
}