Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's C++'s `using` equivalent in golang

Tags:

c++

namespaces

go

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?

like image 677
cookieisaac Avatar asked Aug 11 '16 19:08

cookieisaac


People also ask

What does <- mean in golang?

"=" is assignment,just like other language. <- is a operator only work with channel,it means put or get a message from a channel. channel is an important concept in go,especially in concurrent programming.

What is underlying type in Go?

Underlying typeEach type in Go has something which is called an underlying type. Universe block contains some predeclared identifiers binding to types like boolean, string or numeric. For each predeclared type T its underlaying type is T (no trap here).

How do you find the type of variable in Go?

We can use %T string formatting flag to get type of variable in Go. This is the simplest way to check of find the type of variable in Go language. '%T' flag is part of “fmt” package, we can use it along with fmt. Printf to display the type of variable.


1 Answers

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.

like image 118
Roland Illig Avatar answered Oct 19 '22 08:10

Roland Illig