gpt4 book ai didi

go - 之后不使用他们的名字加载包内容

转载 作者:IT王子 更新时间:2023-10-29 01:27:06 24 4
gpt4 key购买 nike

有没有办法在不需要使用包名称的情况下加载包的内容?例如,在 Python 中你可以这样做:

from somepackage import *
# access function from somepackage foo
foo()

我想在 Go 中做到这一点。我试过:

import _ "path/to/my/package"

但是没有用。如果有的话,我很难清楚地表达自己在网上找到解决方案。

最佳答案

The Go Programming Language Specification

Import declarations

If an explicit period (.) appears instead of a name, all the package's exported identifiers declared in that package's package block will be declared in the importing source file's file block and must be accessed without a qualifier.

使用句点 (.) 代替名称。例如,

package main

import (
"fmt"
. "time"
)

func main() {
fmt.Println(Now()) // time.Now()
}

输出:

2009-11-10 23:00:00 +0000 UTC

关于go - 之后不使用他们的名字加载包内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35518548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com