The use of packages in Golang project

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
    1. 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}
    1. 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}
    1. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.