问题
When I import a package in a go application (using modules and vscode with golang plugin):
import (
cors "goa.design/plugins/cors/dsl"
)
go automatically retrieves the package.
How does go know where to get it from?
I figured it simply pulls the sources from github.com, but the repository for this project is at https://github.com/goadesign/goa
no .
in the name.
And if I change the import to:
import (
cors "goa.design/plugins/v3/cors/dsl"
)
It retrieves the v3
package. I would gladly RTFM, but not sure which FM I need to read.
Btw: this is my second day on go
so this is magic to me.
回答1:
It's simple, check it out:
$> 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}">
The key here is in the tag named "go-import". When go get
requests https://goa.design/..., it hits that HTML file and knows that "goa.design/...." must be retrieved using git from https://github.com/goadesign/plugins.
Here is a good article
来源:https://stackoverflow.com/questions/56290279/how-does-go-know-where-to-get-packages-from