英文:
expose a function in go package
问题
我想直接从一个包中暴露一个函数。这样我就可以直接调用我的包,而不是mypackage.Somepublic()方法。
在node.js中,例如,你可以暴露一个匿名函数
module.export = function() {
console.log('ta da!');
}
或者一个对象
module.export = {
doMagic: function() {
console.log('ta da!');
}
};
英文:
I would like to expose a function directly from a package.
So I could call my package directly instead of mypackage.Somepublic() method.
package main
import (
"mypackage"
"fmt"
)
func main() {
var result = mypackage()
fmt.Println(result)
}
In node.js, for example, you can expose a anonymous function
module.export = function() {
console.log('ta da!');
}
or an object
module.export = {
doMagic: function() {
console.log('ta da!');
}
};
答案1
得分: 25
虽然你的Node.js示例没有直接的类比,但在Go中你可以做一种叫做“本地导入”的事情。基本上,本地导入将包导出的所有项目(函数、类型、变量等)导入到本地命名空间中,以便可以像本地定义一样访问它们。你可以通过在包名前加上一个点来实现这一点。例如:
import . "fmt"
func main() {
Println("Hello!") // 等同于 fmt.Println("Hello!")
}
(在 这里 查看实际效果)。
这对于fmt
导出的任何项目都适用。你可以对mypackage
做类似的事情(这是从你发布的代码修改的):
package main
import (
. "mypackage"
"fmt"
)
func main() {
var result = Somepublic() // 等同于 mypackage.Somepublic()
fmt.Println(result)
}
英文:
While there is no direct analogy for your Node.js example, what you can do in Go is something called a "local import." Basically, a local import imports all of the items - functions, types, variables, etc - that a package exports into your local namespace so that they can be accessed as if they had been defined locally. You do this by prefacing the package name with a dot. For example:
import . "fmt"
func main() {
Println("Hello!") // Same as fmt.Println("Hello!")
}
(See this in action).
This will work for any item that fmt
exports. You could do a similar thing with mypackage
(this is modified from the code you posted):
package main
import (
. "mypackage"
"fmt"
)
func main() {
var result = Somepublic() // Equivalent to mypackage.Somepublic()
fmt.Println(result)
}
答案2
得分: 21
引用mko的答案以提高可见性:
亲爱的孤独的匿名冒险家。我猜你来到这里的原因和我一样。所以,基本答案是。在名称中使用大写字母;) 来源:tour.golang.org/basics/3 - 是的,我也感到惊讶。我期望有些花哨的东西,但那就是全部。大写字母 - 导出,小写字母 - 不导出;)
英文:
Quoting the answer of mko here for better visibility:
> Dear Lonely Anonymous adventurer. I guess you got here for the same reason I did. So, the basic answer is. Use capital letter in the name source: tour.golang.org/basics/3 - yes, I was surprised as well. I was expecting some fancy stuff, but that's all it is. Capital letter - export, small letter - no export
答案3
得分: 8
package main
import (
"fmt"
"./mypackage"
)
func main() {
var value mypackage.Export
value.DoMagic()
fmt.Printf("%s", value)
}
package mypackage
import "fmt"
type Export struct {
}
func (c Export) DoMagic() {
fmt.Println("Magic function was called")
}
func (c Export) String() string {
return fmt.Sprint("ta da! \n")
}
Run go run main.go
you would get
Magic function was called
ta da!
英文:
You need to get your structure currently .. I want to assume your content is in src
folder
src/main.go
package main
import (
"fmt"
"./mypackage"
)
func main() {
var value mypackage.Export
value.DoMagic()
fmt.Printf("%s", value)
}
src/mypackage/export.go
package mypackage
import "fmt"
type Export struct {
}
func (c Export) DoMagic() {
fmt.Println("Magic function was called")
}
func (c Export) String() string {
return fmt.Sprint("ta da! \n")
}
Run go run main.go
you would get
Magic function was called
ta da!
答案4
得分: 4
Go Packages不是你可能认为的那样。
Go程序是通过链接包来构建的。一个包由一个或多个源文件构成,这些源文件一起声明了属于该包的常量、类型、变量和函数,并且可以在同一个包的所有文件中访问。这些元素可以被导出并在另一个包中使用。
这与#include
等不同。
英文:
Go Packages are not what you're perhaps thinking they are.
> Go programs are constructed by linking together packages. A package in turn is constructed from one or more source files that together declare constants, types, variables and functions belonging to the package and which are accessible in all files of the same package. Those elements may be exported and used in another package.
It's not the same as e.g. #include
etc.
答案5
得分: 2
The folder structure should be similar to below.
-mymodule
-mymodule/main.go
-mymodule/mypackage
-mymodule/mypackage/mypack.go
main.go
package main
import (
"example.com/mymodule/mypackage"
"fmt"
)
func main()
{
var result = mypackage.DoMagic //With a first letter i.e 'D' capital
fmt.Println(result)
}
mypack.go
package mypackage
import ( "fmt" )
func DoMagic () string {
fmt.Println("Inside DoMagic")
return "magic Done"
}
Output:
Inside DoMagic
magic Done
英文:
The folder structure should be similar to below.
-mymodule
-mymodule/main.go
-mymodule/mypackage
-mymodule/mypackage/mypack.go
main.go
package main
import (
"example.com/mymodule/mypackage"
"fmt"
)
func main()
{
var result = mypackage.DoMagic //With a first letter i.e 'D' capital
fmt.Println(result)
}
mypack.go
package mypackage
import ( "fmt" )
func DoMagic () string {
fmt.Println("Inside DoMagic")
return "magic Done"
}
Output:
Inside DoMagic
magic Done
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论