The style guide for the pack in the Go language

Source: Internet
Author: User
Tags email string file handling http cookie
This is a creation in Article, where the information may have evolved or changed. The Go language also has its own naming and code organization rules. Beautiful code, layout clear, easy to read and understand, like a rigorous design of the API. Get the code, the user first see and touch is the layout, naming and the structure of the package. This article is not to set up hard rules for you, but to use practice to guide everyone to form their own rules. Because specific problems to be specific analysis, through their own judgment to pick out the most appropriate rules. # # All the Go codes are organized in package form. A package in Go is a directory or folder containing one or more files ending with '. Go '. Use a directory or folder to manage your code in the same way that your computer manages a directory or folder. All Go codes are placed in the package and can only be accessed through a package. The habit of understanding and building good packages can help write efficient code. * * *## Package organization Let's talk about how to organize the Go Code, and explain the specifications for locating the go package. # # # Use multiple files a package is a directory of one or more files. First cut the code into logical and easy-to-read multiple files. For example, depending on the content of the file handling HTTP, an HTTP packet can be cut into multiple files. In the following example, an HTTP packet is cut into the following files: header type definition and code, cookie type definition plus code, and implementation of the actual HTTP functionality and package documentation. -DOC.GO//Package Documentation-HEADERS.GO//HTTP header type definition and code-COOKIES.GO//HTTP cookie type definition and code-HTTP.GO//HTTP client implementation, request and return type, etc. # # The primary rule for aggregation type definitions is to aggregate the type definitions as far as they are referenced. This makes it easier for the maintainer of the code (not just the original author of the code) to find the definition of the type. For example, the head structure type is best placed in the ' headers.go ' file. $ cat Headers.go Package HTTP//header represents an HTTP header struct body definition type header struct {...} Although the Go language itself does not strictly require you to define a type in which part of the file, it is true that the definition of the core type is placed at the top of the file. # # # is arranged according to the function in other languages, usually the type definition is aggregated into a package, called a model or a category. In the Go language, it is arranged through the functional responsibilities of the code. "' Gopackage models//Don't call this nameUser represents one of the users in the system, type user struct {...} "Do not create a package named models, and then define all of the entity types in it. In this example, the User type should be defined in a package in the service layer. "' Gopackage mngtservice//user represents a customer in the system type user struct {...} Func usersbyquery (CTX context. Context, Q *query) ([]*user, *iterator, error) Func Useridbyemail (CTX context. Context, email string) (int64, Error) ' # # # optimization Godoc The sooner you use godoc the better, especially at the initial design of the package API. With Godoc, you can clearly see what your idea is in terms of documentation. Sometimes, visualization also has an impact on design. Because the Godoc needs to be placed in a separate package, it can be optimized slowly to make the document easier to understand. Execute the command ' godoc-http=

Via:https://rakyll.org/style-packages

Author: rakyll Translator: Chenbrooks proofreading: polaris1119

This article by GCTT original compilation, go language Chinese network honor launches

This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove

1142 reads ∙1 likes
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.