go 如何知道从哪里获取包

How does go know where to get packages from

当我在 go 应用程序中导入包时(使用模块和 vscode 以及 golang 插件):

import (
   cors "goa.design/plugins/cors/dsl"
)

go 自动检索包。 go 如何知道从哪里得到它? 我认为它只是从 github.com 中提取源代码,但该项目的存储库位于 https://github.com/goadesign/goa 名称中没有 .。 如果我将导入更改为:

import (
   cors "goa.design/plugins/v3/cors/dsl"
)

它检索 v3 包。我很乐意使用 RTFM,但不确定我需要阅读哪个 FM。

顺便说一句:这是我在 go 上的第二天,所以这对我来说很神奇。

很简单,看一看:

$> curl https://goa.design/plugins/cors/dsl
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-us">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">


  <meta name="go-import" content="goa.design/plugins git https://github.com/goadesign/plugins">
  <meta name="go-source" content="goa.design/plugins _ https://github.com/goadesign/plugins/tree/master/{/dir} https://github.com/goadesign/plugins/blob/master{/dir}/{file}#L{line}">

这里的关键在名为 "go-import" 的标签中。当 go get 请求 https://goa.design/..., it hits that HTML file and knows that "goa.design/...." must be retrieved using git from https://github.com/goadesign/plugins.

这里有个好东西article