Go   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Golang语言空白符大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

空白符(blank identifier)

空白符的产生可能是因为go不允许变量声明了但不使用。既然不想使用,何必声明变量呢,那就将变量用空白符代替,反正空白符就是用来抛弃的。

1 空白符用来匹配一些不需要的值,然后丢弃掉,下面的 blank_identifier.go 就是很好的例子。

@H_197_7@ThreeValues是拥有三个返回值的不需要任何参数的函数,在下面的例子中,我们将第一个与第三个返回值赋给了i1f1。第二个返回值赋给了空白符_,然后自动丢弃掉。

示例 6.4blank_identifier.go

package main

import "fmt"

func @H_263_32@main() {
    var i1 int
    f1 float32
    i1,_,f1 = ThreeValues()
    fmt.Printf("ThE int: %d,the float: %f \n",i1,f1)
}

ThreeValues() (int,float32) {
    return 5,6,179)">7.5
}

输出结果:

ThE int: 5,the float: 7.500000

2 import时候加空白符

package main

import(
  "image"
  "image/jpeg" // I wanted to export the images as JPEG
  _ "image/png"
  _ "image/gif"
)

// ...
表示,只使用被引用包的init函数,只导入png,gif包但不使用,又为避免不使用的错误断定。

3

@H_874_86@type T struct{} var _ I = T{} // Verify that T implements I. 这种情况类似静态断言,断定T{}可以是接口,但又不想再使用结果I.

4

func (env *maasEnviron) Bootstrap(ctx environs.bootstrapContext,args environs.bootstrapParams) (arch,series String,_ environs.bootstrapFinalizer,_ error) {

这种情况?在使用时仍然会正常使用finalizer和err。FIXME

@H_404_96@

@H_404_96@

@H_404_96@

@H_404_96@参

@H_404_96@1 资料集合

@H_404_96@http://blog.csdn.net/loongshawn/article/details/54112640

@H_404_96@

@H_404_96@2 空白符

@H_404_96@https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/06.2.md

@H_404_96@3

@H_404_96@http://stackoverflow.com/questions/26972615/a-use-case-for-imporTing-with-blank-identifier-in-golang

@H_404_96@To import a package solely for its side-effects (initialization),use the blank identifier as explicit package name.

@H_404_96@@H_674_115@4

@H_404_96@@H_674_115@http://stackoverflow.com/questions/24357028/meaning-of-underscore-blank-identifier-in-go

@H_404_96@@H_674_115@@H_674_115@There is no way to access this variable so it will be optimised out of the resulTing program. However,it Could cause a compile error if the typeT@H_674_115@is not assignable to thE interfaceI@H_674_115@. So in this case it is being used as a static assertion about a type.

@H_404_96@@H_674_115@

@H_404_96@@H_674_115@5 go例子

@H_404_96@@H_674_115@https://gobyexample.com/

@H_404_96@PS 使用litEIDE进行编程。litEIDE查找比较方便,关联和其他引用高亮离source insight差很多

大佬总结

以上是大佬教程为你收集整理的Golang语言空白符全部内容,希望文章能够帮你解决Golang语言空白符所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。