如何将Go包导入为下划线“_”?

huangapple go评论94阅读模式
英文:

How to import go package as underscore "_"?

问题

由于在Go语言中,_被用作空白标识符以丢弃值。下面的导入语句将导入该包并执行其init函数。是否有一种方法可以将该包别名为_,这样我就可以使用_.Method()来调用它。

import (
    _ "github.com/ziutek/mymysql/godrv"
)
英文:

Since _ is used in Go as a blank identifier to discard value. The import statement below will import the package and execute its init function. Is there a way to alias the package as _? so that I can use it as _.Method().

import (
    _ "github.com/ziutek/mymysql/godrv"
)

答案1

得分: 1

不,根据你描述的原因,这是不可能的。最接近的方法是使用两个下划线:

import (
    __ "github.com/ziutek/mymysql/godrv"
)

<!-- -->

__.Method()
英文:

No, this is not possible, for the reason you described. The closest you can do is use two underscores:

import (
    __ &quot;github.com/ziutek/mymysql/godrv&quot;
)

<!-- -->

__.Method()

huangapple
  • 本文由 发表于 2016年3月26日 02:37:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/36225880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定