问题描述
什么是C ++的使用some_namespace :: object
在golang中是等效的?
根据
我可以使用命名空间common 来获得与下面的声明:
pre $ import(
。common
)
但是这会导入整个名称空间。现在我只想使用
platform
定义,例如使用common :: platform
是否有与Go类似的功能,所以我不必一直输入 common.platform
p>
解决方案以下代码在可读性方面非常接近,但效率较低,因为编译器无法再内联函数调用。 >
导入(
fmt
字符串
)
var(
Sprintf = fmt.Sprintf
HasPrefix = strings.HasPrefix
)
并且,它具有将名称 fmt
和 strings
导入文件范围的副作用,这是C ++的使用
不能做的事情。
What's C++'s using some_namespace::object
equivalent in golang?
According to the question hereI can get using namespace common
with statement below:
import (
. "common"
)
But this would import the entire namespace. Right now I only want to use, say platform
definition, something like using common::platform
Is there an equivalent for this in Go, so that I don't have to type common.platform
all the time?
解决方案 The following code comes close in terms of readability, but is less efficient, since the compiler cannot inline function calls anymore.
import (
"fmt"
"strings"
)
var (
Sprintf = fmt.Sprintf
HasPrefix = strings.HasPrefix
)
And, it has the side-effect of importing the names fmt
and strings
into the file scope, which is something that C++'s using
does not do.
这篇关于Golang中C ++的“使用”等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!