golang常見錯誤

來源:互聯網
上載者:User

import

import unuse package:error : imported and not used: "os" 

:= =

c := 1 // error non-declaration statement outside function bodyd = 1 // error non-declaration statement outside function body

func test(){
c = 1 //undefined: should be c:=1
//d = 1
var f , d int
f,d = cal()
fmt.Println(c, f, d)
}

right using value declare and use

package mainimport "fmt"//var a := 0 //wrongvar a int = 0//c := 1 //wrong//d = 1 //wrongfunc cal(b int)(val1 int, val2 int){  fmt.Println(b)  val1 = 1  val2 = 2  return }func test(){  c := 1  //d = 1 //wrong  var f , d int  f,d = cal(1)  fmt.Println(c, f, d)}func main() {  fmt.Println("Hello, world var")}

declear not use

./right.go:14: c declared and not used  

type struct init using () ,instead of {} ,which {} is the right usage

type Response struct {  Code string `json:"code"`  Body string `json:"body"`}//not like this ()//Response("OK", "ECHO: " + method + " ~ " + params)//right usage {}Response{"OK", "ECHO: " + method + " ~ " + params}

如何理解以下代碼:

type IpcClient struct {  conn chan string}func (client *IpcClient)Call(method, params string)(resp *Response, err error){  }
  • (client *IpcClient) -- 調用的對象要是 IpcClient struct
  • (method, params string) -- 參數,前面是參數名,後面是參數類型,兩種同類別省略寫法。
  • (resp *Response, err error) 參數傳回值,傳回值的第一個參數,類型為 Response struct 對象指標, 傳回值的第二個參數,類型為 error 類型,

wrong not using go keyworld when call async func

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.