gpt4 book ai didi

go - Go Lang 中的重复导入

转载 作者:IT王子 更新时间:2023-10-29 01:44:17 26 4
gpt4 key购买 nike

在我导入这个的场景中:

import (
"fmt"
"github.com/julienschmidt/httprouter"
"net/http"
"log"
)

但是 httprouter/router.go 导入:

import (
"net/http"
)

我还需要在我的 go 文件中导入“net/http”吗?

最佳答案

您的文件只需要关心在那个特定文件 中使用了哪些导入。

您可以尝试使用 goimports看看它如何建议要导入的东西。 Effective Go有这样的话:

When a package is imported, the package name becomes an accessor for the contents. After import "bytes" the importing package can talk about bytes.Buffer.

迂腐地说,这并不完全正确,因为您可以在同一个包中有多个文件,每个文件都需要导入它需要的任何内容(例如,一个包中的 2 个文件可能每个 import fmt

关于go - Go Lang 中的重复导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50863031/

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