关于在go语言中导入当前文件夹的步骤

Python019

关于在go语言中导入当前文件夹的步骤,第1张

1.先把要导入的包go build - go install

2.接着在要导入的方法中以: 项目名 / 导入的文件名  的形式导入即可(如下方test(项目名) / chiid(文件名))

go/src/go-cve-dictionary-master

# mv subcommands-master /opt/go/src/subcommands

# mv net-master /opt/go/src/net

# mv go-sqlite3-master /opt/go/src/go-sqlite3

都放到了go/src目录下了,我还修改了go-cve-dictionary-master/main.go文件内容,如下所示:

import (

"flag"

"fmt"

"os"

"golang.org/x/net/context"改为 “context”

"github.com/google/subcommands" 改为 subcommands

"github.com/kotakanbe/go-cve-dictionary/commands" 改为 go-cve-dictionary/commands

"github.com/kotakanbe/go-cve-dictionary/version" 改为 go-cve-dictionary/version

_ "github.com/mattn/go-sqlite3" 改为 go-sqlite3

)

执行 # go install go-cve-dictionary-master 错误如下:

can't load package: /opt/go/src/go-cve-dictionary-master/main.go:14:2: non-standard import "github.com/mattn/go-sqlite3" in standard package "go-cve-dictionary-master"

go-cve-dictionary-master/main.go:11:2: cannot find package "go-cve-dictionary/commands" in any of:

/opt/go/src/vendor/go-cve-dictionary/commands (vendor tree)

/opt/go/src/go-cve-dictionary/commands (from $GOROOT)

/root/go/src/go-cve-dictionary/commands (from $GOPATH)

go-cve-dictionary-master/main.go:12:2: cannot find package "go-cve-dictionary/version" in any of:

/opt/go/src/vendor/go-cve-dictionary/version (vendor tree)

/opt/go/src/go-cve-dictionary/version (from $GOROOT)

/root/go/src/go-cve-dictionary/version (from $GOPATH)

subcommands/subcommands.go:29:2: cannot find package "golang.org/x/net/context" in any of:

/opt/go/src/vendor/golang.org/x/net/context (vendor tree)

/opt/go/src/golang.org/x/net/context (from $GOROOT)

/root/go/src/golang.org/x/net/context (from $GOPATH

二进制包安装的话:如果安装在了默认位置例如/usr/local/go (Windows系统:c:\Go)这个时候可以不需要设置GOROOT,如果改变了安装的这个默认目录,那么就需要设置GOROOTGOROOT的概念有点Go安装目录的意思GOPATH是一个开发环境目录的意思,下面必须包含bin、pkg、src,然后再src下面新建项目就可以了你在没有设置GOPATH的情况下,也可以把项目建在GOROOT/src下面,其实和GOPATH的概念类似