Golang初級系列教程-常見錯誤

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

知行易,知其不行難。首先,在這片文章中指出常見的一些錯誤,避免你在之後的編程中無所謂的調試工作。

    • 多餘的 Imports
    • 命名區分大小寫
    • 分號分行
    • 無效的分號
    • 文法和其它問題

多餘的 Imports

建立檔案,將如下內容拷貝並執行

ErrProg1.go

package mainimport "fmt"import "os" //excessive - we are not using any function in this packagefunc main() { fmt.Println("Hello world")}

輸出為:

prog.go:4: imported and not used: os

Go編譯器對Go程式非常嚴格,如果你不使用,就不要有多餘的請求。在上面的代碼中,試圖引入os包,但是在代碼當中,並沒有使用,Go編譯器對這樣的行為嚴厲禁止。去掉第四行代碼之後,程式就能正確編譯運行。

命名區分大小寫

ErrProg2.go

package mainimport "fmt"func main() { fmt.println("Hello world")}

輸出為:

prog.go:6: cannot refer to unexported name fmt.printlnprog.go:6: undefined: fmt.println

上述代碼中,列印函數寫的是fmt.println不是之前所寫的fmt.PrintlnGo語言區分大小寫,所以在編程時,要嚴格按照定義的方式進行引用和調用。以下代碼都是不正確的:

Package mainiMport "fmt"import "Fmt"Func main() {}Fmt.Printlnfmt.println

分號分行

如果你學過CC++JavaPerl等等,應該已經注意到Go(至少在前面的代碼中)沒有要求在語句的末尾添加分號。其實在Go語言中,會自動在一行的末尾添加分號。然而,如果在一樣有兩條運算式,需要用分號顯示的進行分割。讓我們舉個栗子:

ErrProg3.go

package mainimport "fmt"func main() { fmt.Println("Hello world") fmt.Println("Hi again")}

輸出:

prog.go:6: syntax error: unexpected name, expecting semicolon or newline or }

解決以上問題,可以將上述的兩條語句放在兩行

部分代碼

func main() { fmt.Println("Hello world")            fmt.Println("Hi again")}

為了說明分號的作用,我們使用以下方式進行修改

package mainimport "fmt"func main() { fmt.Println("Hello world"); fmt.Println("Hi again")

輸出:

Hello worldHi again

因此在Go語言中,分號能省則省,如果必須使用時,添加上也不會出錯。所以,如下代碼也是正確滴。

ErrProg4.go

package main;import "fmt";func main() { fmt.Println("Hello world"); fmt.Println("Hi again");};

輸出為:

Hello worldHi again

但是也請大家注意這些自動產生的分號。

無效的分號

繼續修改上述代碼

package mainimport "fmt";;func main() { fmt.Println("Hello world")}

輸出為:

prog.go:3: empty top-level declaration

上述問題得原因出現在import後面的第二個分號,第一個分號是正確的,上面的代碼已經驗證過。第二個分號之前沒有任何有效運算式,所以編譯器報了上述的錯誤。去掉多餘的分號,程式可正確運行。

文法和其它問題

每種語言都有各自的文法要求,Go編譯器也不例外。很多時候我們會犯一些語法錯誤,以下會列出一些,以供大家參考。

譯者註:以下錯誤輸出就不在翻譯了,保持錯誤輸出原汁原味更好,這對大家應該不是問題)

package 'main' //ERROR - no quotes for the package name: package mainpackage "main" //ERROR - no quotes for the package: package mainpackage main.x  //ERROR - packages names in go are just one expression.  So either package main or package x.package main/x  //ERROR - packages names in go are just one expression.  So either package main or package x.import 'fmt' //ERROR - needs double quotes "fmt"import fmt //ERROR - needs double quotes "fmt"func main { } //ERROR - functions have to be followed by parantheses: func main() {}func main() [] //ERROR - where curly braces are required, only those are allowed.  They are used to contain blocks of code.  func main() {}func main() { fmt.Println('hello world') } //ERROR - use double quotes for strings: func main() { fmt.Println("hello world") }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.