常见语法错误
最后更新于:2022-04-02 04:55:37
`"{" 换行: Opening Brace Can't Be Placed on a Separate Line `
错误:
~~~
package main
func main()
{
println("hello world")
}
~~~
报错信息:
~~~
./main.go:3:6: missing function body for "main"
./main.go:4:1: syntax error: unexpected semicolon or newline before {
~~~
正确:
~~~
package main
func main() {
println("hello world")
}
~~~
`定义未使用的变量: Unused Variables`
错误:
~~~
package main
var str string
func main() {
var num int
println("hello world")
}
~~~
报错信息:
`./main.go:6:6: num declared and not used`
正确:
~~~
package main
var str string
func main() {
// var num int
println("hello world")
}
~~~
`import 但未使用: Unused Imports`
错误:
~~~
package main
import (
"fmt"
"os"
)
func main() {
println("hello world")
}
~~~
报错信息:
~~~
./main.go:4:2: imported and not used: "fmt"
./main.go:5:2: imported and not used: "os"
~~~
正确:
~~~
package main
import (
"fmt"
_ "os"
)
func main() {
fmt.Println("hello world")
}
~~~
`命名区分大小写`
错误:
~~~
package main
import "fmt"
func main() {
fmt.println("Hello world")
}
// 以下代码都是不正确的:
// Package main
// iMport "fmt"
// import "Fmt"
// Func main() {}
// Fmt.Println
// fmt.println
~~~
报错信息:
~~~
./main.go:6:2: cannot refer to unexported name fmt.println
./main.go:6:2: undefined: fmt.println
~~~
正确:
~~~
package main
import "fmt"
func main() {
fmt.Println("Hello world")
}
~~~
`分号分行`
错误:
~~~
package main
import "fmt"
func main() {
fmt.Println("Hello world") fmt.Println("Hi again")
}
~~~
错误信息:
./main.go:6:29: syntax error: unexpected fmt at end of statement
正确:
~~~
package main
import "fmt"
func main() {
fmt.Println("Hello world")
fmt.Println("Hi again") //解决以上问题,可以将上述的两条语句放在两行
fmt.Println("Hello world");fmt.Println("Hi again") //可以将两条语句用分号结束
test()
}
func test() {
//因此在Go语言中,分号能省则省,如果必须使用时,添加上也不会出错。所以,如下代码也是正确滴。
fmt.Println("Hello world");fmt.Println("Hi again");
};
~~~
`无效的分号`
错误:
~~~
package main
import "fmt";;
func main() {
fmt.Println("Hello world")
}
~~~
错误信息:
~~~
./main.go:3:14: syntax error: non-declaration statement outside function body
~~~
正确:
~~~
package main
import "fmt";
func main() {
fmt.Println("Hello world")
}
~~~
简短变量定义方式只能在函数内部使用: Short Variable Declarations Can Be Used Only Inside Functions
错误:
~~~
package main
str := "hello world"
func main() {
println(str)
}
~~~
报错信息:
`./main.go:3:2: syntax error: non-declaration statement outside function body`
正确:
~~~
package main
func main() {
str := "hello world"
println(str)
}
~~~
重复定义 变量: Redeclaring Variables Using Short Variable Declarations
错误:
~~~
package main
var num int
var num int
func main() {
str := "hello world"
str := "hello golang"
println(str)
}
~~~
报错信息:
~~~
./main.go:4:5: num redeclared in this block
previous declaration at ./main.go:3:5
./main.go:8:6: no new variables on left side of :=
~~~
正确:
~~~
package main
var num int
var num2 int
func main() {
str := "hello world"
str = "hello golang"
println(str)
}
~~~
注意变量作用域: Accidental Variable Shadowing
错误:
~~~
package main
var num int
func main() {
str := "hello world"
if true {
var b bool
}
println(num)
println(str)
println(b)
}
~~~
报错信息:
`./main.go:12:10: undefined: b`
正确:
~~~
package main
var num int
func main() {
str := "hello world"
if true {
var b bool
println(b)
}
println(num)
println(str)
// println(b)
}
~~~
nil 不能赋值给 不明类型的变量和非指针类型的变量: Can't Use "nil" to Initialize a Variable Without an Explicit Type
错误:
~~~
package main
var null string = nil
func main() {
null2 := nil
println(null)
println(null2)
}
~~~
报错信息:
~~~
./main.go:3:5: cannot use nil as type string in assignment
./main.go:6:8: use of untyped nil
~~~
正确:
~~~
package main
var null *string = nil
func main() {
var null2 *int
null2 = nil
println(null)
println(null2)
}
~~~
map 未初始化 直接使用: Using "nil" Slices and Maps------interfaces, functions, pointers, maps, slices, and channels
错误:
~~~
package main
import (
"fmt"
)
func main() {
var m map[int]int
m[1] = 1
fmt.Println(m)
}
~~~
报错信息:
~~~
panic: assignment to entry in nil map
~~~
正确:
~~~
package main
import (
"fmt"
)
func main() {
var m map[int]int
m = make(map[int]int)
m[1] = 1
fmt.Println(m)
}
~~~
map 只有 len操作, 没有 cap 操作: Map Capacity
错误:
~~~
package main
import (
"fmt"
)
func main() {
m := map[int]string{1: "a", 2: "b", 3: "c"}
cap := cap(m)
fmt.Println(cap)
}
~~~
报错信息:
~~~
./main.go:9:12: invalid argument m (type map[int]string) for cap
~~~
正确:
~~~
package main
import (
"fmt"
)
func main() {
m := map[int]string{1: "a", 2: "b", 3: "c"}
len := len(m)
fmt.Println(len)
}
~~~
';