处理Golang中的重复声明导入

huangapple go评论68阅读模式
英文:

Golang | Handle duplicate declaration on import

问题

以下是要翻译的内容:

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"os"

	"github.com/HewlettPackard/docker/api/client"
	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/container"
	"github.com/docker/docker/client"
)

在这里,关注这两个特定的库:

"github.com/HewlettPackard/docker/api/client"
"github.com/docker/docker/client"

它们都返回一个"client",我如何重写其中一个的名称并同时使用这两个库。

提前感谢!

英文:
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"os"

	"github.com/HewlettPackard/docker/api/client"
	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/container"
	"github.com/docker/docker/client"
)

Here, focues on these 2 specific libs :

"github.com/HewlettPackard/docker/api/client"    	
"github.com/docker/docker/client"

both of them return a "client",
how can I override name of one of those 2 and use both the libraries at once.

Thanks in advance !

答案1

得分: 2

你可以通过以下方式为每个包命名:

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "io"
    "os"

    hpclient "github.com/HewlettPackard/docker/api/client"
    "github.com/docker/docker/api/types"
    "github.com/docker/docker/api/types/container"
    dockerclient "github.com/docker/docker/client"
)

现在你可以在访问这些包时使用hpclientdockerclient这些名称。你可以使用任何你感觉舒适的名称。

英文:

You can give a name to each package in the following way -

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "io"
    "os"

    hpclient "github.com/HewlettPackard/docker/api/client"
    "github.com/docker/docker/api/types"
    "github.com/docker/docker/api/types/container"
    dockerclient "github.com/docker/docker/client"
)

Now you can use the names hpclient or dockerclient while accessing the packages. You can use any names which are comfortable for you.

huangapple
  • 本文由 发表于 2021年9月7日 20:12:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/69088043.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定