GO“拆分”程序包配置设置

Go newbie alert:

I've created a package that I want to split up into several individual files, but I wanted to have the layout like so:

\--src
    |---myLib.go
    |---myLib
          |--- file1.go
          |--- file2.go
          | ...
          |--- fileN.go

Each .go file has "package myLib" at the top, but I'm not sure how to get the pathing to work. The main file "myLib.go" contains the "package globals" and high level function calls (exported), while the file1, ..., fileN files essentially contain the "private" non-exported functions used internally by the exported functions.

Not sure how to setup the imports, every combination I've tried I get the message: "no such file or directory" when I try to include the files in the myLib folder. If I put all the files at the same level, then it works fine. From an organizational standpoint, I prefer to have the one file with the exported functions separate and all the non-exported functions/files consolidated in a sub-folder. Any suggestions / examples?

Thx

You cannot do this. You must do it like it is intended:

The file myLib.go must go in the myLib folder. (But you may call the folder whatever you like.)

Have a look at how the standard lib is organized.

The key piece is: all the .go and _test.go files in the same directory should start with the same package <packagename> statement. The .go files in the directory that contains a file with the main function should all have package main statement - they are used to create an executable (named with the directory name).

the file1, ..., fileN files essentially contain the "private" non-exported functions used internally by the exported functions.

Only the upper-cased function and type names can be used in other packages (including in package main). The setup you have should work as long as all the .go files in the same directory share the same package <pkgname> statement - for example, if you put package mylib in file1.go, file2.go... fileN.go (I would also recommend to avoid uppercase directory names to avoid confusion).

It is also worth to review the "How to Write Go Code" document https://golang.org/doc/code.html or the screencast linked from it, should help with the Go codebase layout conventions.