使用 cmd 和 pkg 布局去构建项目 - 构建错误

Go build project with cmd and pkg layout - build error

我正在尝试使用 Go Project Layout

中描述的布局构建 Go 项目

我在 Ubuntu 上使用 go 1.9.2。我的项目布局如下

$GOPATH/src/github.com/ayubmalik/cleanprops
    /cmd
        /cleanprops
            /main.go
    /internal
        /pkg
            /readprops.go

文件 cmd/cleanprops/main.go 指的是 cleanprops 包,即

package main

import (
    "fmt"
    "github.com/ayubmalik/cleanprops"
)

func main() {
    body := cleanprops.ReadProps("/tmp/hello.props")
    fmt.Println("%s", body)
}

internal/pkg/readprops.go的内容是:

package cleanprops

import (
    "fmt"
    "io/ioutil"
)

func check(e error) {
    if e != nil {
        panic(e)
    }
}

func ReadProps(file string) string {
    body, err := ioutil.ReadFile(file)
    check(err)
    fmt.Println(string(body))
    return body
}

然而,当我构建 cmd/cleanprops/main.go 时,从内部目录 $GOPATH/src/github.com/ayubmalik/cleanprops,使用命令:

go build cmd/cleanprops/main.go 

我收到以下错误:

cmd/cleanprops/main.go:5:2: no Go files in /home/xyz/go/src/github.com/ayubmalik/cleanprops

我错过了什么?

文档建议采用这种结构:

$GOPATH/src/github.com/ayubmalik/cleanprops
    /cmd
        /cleanprops
            /main.go
    /internal
        /pkg
            /cleanprops
                /readprops.go

像这样导入包。导入路径匹配下面的目录结构 $GOPATH/src.

package main

import (
    "fmt"
    "github.com/ayubmalik/cleanprops/internal/pkg/cleanprops"
)

func main() {
    body := cleanprops.ReadProps("/tmp/hello.props")
    fmt.Println("%s", body)
}