This is a creation in Article, where the information may have evolved or changed.
- Different parts of the same package can be placed in multiple files under the same folder.
Package Mainimport"FMT"/*//This place does not have any problems with the same file type rect struct{width, height int}func (r *rect) area () int {return r.width * R.height}func (R *rect) Perim () int {return 2 * r.width + 2 * r.height}*/Func Main () {r: = rect{width:Ten, Height:5} FMT. Println("Area:"R. Area()) FMT. Println("Perim:"R. Perim()) RP: = &r FMT. Println("Area:"Rp. Area()) FMT. Println("Perim:"Rp. Perim()) }
Some of the above comments can also be placed in other files under the same folder, but must be in the main package
package maintypestruct{ int }funcint { return r.width * r.height}funcint { return 2 * r.width + 2 * r.height}
- Can be split into different packages, and the main package in the same directory, such a package can only be used by themselves, can not be shared to other projects
Package Mainimport"FMT"The path to the imported package is relative to the SRC directory, and the main package is also under the test file import"Test/rect" /*type rect struct{width, height int}func (r *rect) area () int {return r.width * R.height}func (R *rect ) Perim () int {return 2 * r.width + 2 * r.height}*/Func Main () {r: = rect. Rect{Width:Ten, Height:5} FMT. Println("Area:"R. Area()) FMT. Println("Perim:"R. Perim()) RP: = &r FMT. Println("Area:"Rp. Area()) FMT. Println("Perim:"Rp. Perim())}
As a standalone package file, note the appropriate data structures and methods to export
package recttypestruct{ int }funcint { return r.Width * r.Height}funcint { return 2 * r.Width + 2 * r.Height}
- Put the package in the SRC directory of the Gopath to allow all projects to be shared!
Copy the package directory to the SRC directory of Gopath and modify the import The path is good.
Package Mainimport"FMT"This position modifies the import"Rect" /*type rect struct{width, height int}func (r *rect) area () int {return r.width * R.height}func (R *rect ) Perim () int {return 2 * r.width + 2 * r.height}*/Func Main () {r: = rect. Rect{Width:Ten, Height:5} FMT. Println("Area:"R. Area()) FMT. Println("Perim:"R. Perim()) RP: = &r FMT. Println("Area:"Rp. Area()) FMT. Println("Perim:"Rp. Perim())}
Organize the project first, fill in the specific content, find the specific syntax, the logic is clear.