golang常见错误

no7dw · · 2109 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

import

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

:= =

c := 1 // error non-declaration statement outside function body
d = 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 main

import "fmt"

//var a := 0 //wrong
var a int = 0
//c := 1 //wrong
//d = 1 //wrong

func 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


有疑问加站长微信联系(非本文作者)

本文来自:博客园

感谢作者:no7dw

查看原文:golang常见错误

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2109 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传