I think this will work.
---
An error that occurred when I was developing with Go and tried to separate packages using files.
I solved it for the time being, so I made a note immediately. .. ..
## environment
version go version go1.13.14 linux/amd64 centos 7
Directory structure ./ | |--sample/ | | | |--sample.go | |--main.go
## Actual code
#### **`main.go`**
```go
package main
import "./sample"
func main() {
a := sample.Foo("test")
sample.PrintFoo(a)
}
sample.go
package sample
import "fmt"
func Foo(s string) string {
return "aaa " + s + " bbb"
}
func PrintFoo(s string) {
fmt.Println(s)
}
[root@marny ---]# go build -o practice
unexpected directory layout:
import path: _/develop/go/src/test/marny/sample
root: /develop/go/src
dir: /develop/go/src/test/marny/sample
expand root: /develop/go
expand dir: /develop/go/src/test/marny/sample
separator: /
The error ʻunexpected directory layout:` seems to be an error that the path of the imported package is not specified.
I myself edited centos7 (vbox) by connecting it to windows with samba, so there was no error in the source code ...
In my case, ** $ GOPATH ** is specified as / develop / go
. According to the GOPATH specifications, I generate bins and srcs and develop Go, but an error occurs here.
The solution is to change import./sample
in main.go to the relative path seen from $ GOPATH / src
.
Therefore,
main.go
package main
import "test/marny/sample"
func main() {
a := sample.Foo("test")
sample.PrintFoo(a)
}
I think this will solve it.
GOPATH is annoying.