dtdh11647
2017-12-09 15:57
浏览 172
已采纳

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

I'm trying to build a Go project using the layout as described in Go Project Layout

I'm using go 1.9.2 on Ubuntu. My project layout is as follows

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

The file cmd/cleanprops/main.go is referring to the cleanprops package i.e.

package main

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

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

The contents of internal/pkg/readprops.go are:

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
}

However when I build cmd/cleanprops/main.go, from inside directory $GOPATH/src/github.com/ayubmalik/cleanprops, using command:

go build cmd/cleanprops/main.go 

I get the following error:

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

What am I missing?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用转到项目布局

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

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

文件cmd / cleanprops / main.go指向cleanprops软件包,即 \ n

 包main 
 
import(
“ fmt” 
“ github.com/ayubmalik/cleanprops"
)

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

内部/ pkg / readprops的内容 .go are:

  package cleanprops 
 
import(
“ fmt” 
“ io / ioutil” 
)
 
func check(e错误){  
 if e!= nil {
 panic(e)
} 
} 
 
func ReadProps(file string)string {
 body,err:= ioutil.ReadFile(file)
 check(err)  
 fmt.Println(string(body))
返回正文
} 
   
 
 

但是,当我从目录内部构建cmd / cleanprops / main.go时 $ GOPATH / src / github.com / ayubmalik / cleanprops,u 唱歌命令:

 去编译cmd / cleanprops / main.go 
   
 
 

我得到以下错误:< /p>

cmd/cleanprops/main.go:5:2:/home/xyz/go/src/github.com/ayubmalik/cleanprops
  
 
 

我想念什么?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doupa8922 2017-12-09 18:00
    已采纳

    The document suggests this structure:

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

    Import the package like this. The import path matches the directory structure below $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)
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题