Golang中C ++的“使用”等价物 [英] What's C++'s `using` equivalent in golang

查看:155
本文介绍了Golang中C ++的“使用”等价物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是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 here I 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 ++的“使用”等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆