如果包名称不能与导入路径的最后一个元素匹配的话,则必须使用导入别名
import (
"net/http"
client "example.com/client-go"
trace "example.com/trace/v2"
)
在所有其他场景中,应该避免导入别名,除非导入之间有直接冲突
Bad
import (
"fmt"
"os"
nettrace "golang.net/x/trace" //此处,应该避免使用导入别名
)
Good
import (
"fmt"
"os"
"runtime/trace"
nettrace "golang.net/x/trace" //有冲突了,需要使用导入别名
)
本文探讨了在Go语言中如何正确使用导入别名来解决包名与导入路径不匹配的问题及避免导入间的冲突。介绍了何时应当使用导入别名,并通过示例展示了正确的用法。


被折叠的 条评论
为什么被折叠?



