Skip to content

Go 下划线的不同用法 #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
git-zjx opened this issue Sep 23, 2020 · 0 comments
Open

Go 下划线的不同用法 #64

git-zjx opened this issue Sep 23, 2020 · 0 comments
Assignees
Labels
Go Go 转载 转载

Comments

@git-zjx
Copy link
Owner

git-zjx commented Sep 23, 2020

原文地址

  1. import 中
import  _  "net/http/pprof"

引入包,会先调用包中的初始化函数,这种使用方式仅让导入的包做初始化,而不使用包中其他功能

  1. 返回值中
_,err := func()

忽略某个返回值

  1. 变量中
type T struct{}
var _ M = T{}

用来判断 type T是否实现了 M,用作类型断言,如果 T 没有实现接口 I,则编译错误

  1. 函数定义中
func demo() ( sumA int, sumB int, _ string, total int ,_ error)

返回参数 int 加了名字,对比上面,便于理解,但是 string 和 error 没有名字,编译会报错,用下划线可以忽略命名

@git-zjx git-zjx added 转载 转载 Go Go labels Sep 23, 2020
@git-zjx git-zjx self-assigned this Sep 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Go Go 转载 转载
Projects
None yet
Development

No branches or pull requests

1 participant