8000 Go 下划线的不同用法 · Issue #64 · git-zjx/git-zjx.github.io · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Go 下划线的不同用法 #64
Open
@git-zjx

Description

@git-zjx

原文地址

  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 没有名字,编译会报错,用下划线可以忽略命名

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0