欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

golang 引用其它目录的包

程序员文章站 2022-05-29 22:18:28
...

golang 导入本地包

源码目录:

[email protected]:~/go-path/src/test# pwd
/root/go-path/src/test
[email protected]:~/go-path/src/test# tree
.
├── go.mod
├── hello
│   └── hello.go
└── main.go

1 directory, 3 files
[email protected]:~/go-path/src/test#

main.go

package main

import (
        "fmt"
        "test/hello"
)

func main() {
        hello.Hello()
        fmt.Println("aa")
}

hello/hello.go

package hello

import (
        "fmt"
)

func Hello() {
        fmt.Println("hello api")
}

环境:

[email protected]:~/go-path/src/test# go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go-path/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go-path"
GOPRIVATE=""
GOPROXY="https://goproxy.io"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/root/go-path/src/test/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build614597735=/tmp/go-build -gno-record-gcc-switches"
[email protected]:~/go-path/src/test# 

编译运行:

[email protected]:~/go-path/src/test# go build
[email protected]:~/go-path/src/test# ./test 
hello api
aa
[email protected]:~/go-path/src/test#