Video Note: Advice to Go library authors-jack Lindamood

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
    • Video information
      • Named
      • Object Creation
      • Log
      • interfaceVsstruct
      • When panic?
      • Check for error
      • Allow the ability to enable library debugging
      • Designed for testing
      • Concurrent
        • Channels
        • When was it launched?goroutine
      • When to use the context. Context
      • Other precautions

Video Info #

Practical Advice for Go Library Authors
by Jack Lindamood
At Gophercon 2016

Https://www.youtube.com/watch?v=5v2fqm_8jYI

Slide Address: http://go-talks.appspot.com/github.com/cep21/go-talks/practical-advice-for-go-library-authors.slide#1

Named

Package names are part of the future use process, so avoid repeating package names and structures and functions. Like what

1
var var h http. Client
1
Context. Newcontext () = context. Background ()

Object Creation #

Golang has no constructors, so there are generally two ways to create objects

    • The default 0 value
    • A separate constructor,NewSomething()

Recommended 0 construction method for default values

In the 0 case of default values, each method handles good 0 values, such as when something is found to be 0 a value, and a default value is given.

New()The constructor is flexible enough to do anything, so it's bad for code reading, which means hiding a lot of things.

Some libraries use private structs, methods that expose interfaces, authImpl struct and Auth interface , which are anti-patterns, are not recommended for use.

It is not recommended to use Singleton, although the Singleton model is used extensively in the standard library, but Jack personally does not like this mode.

Using higher-order functions as options is not recommended in this form:NewSomething(WithThingA(), WithThingB())

Log

Some logs are printed directly to the standard output, which is a very bad design, because if the user wants to shut down at all.

Suggestions

    • Determine if it is really necessary to print the log as a library , should the output log work to the caller decide?
    • If a log must be required, use the callback function method
    • Output logs to ainterface
    • Do not assume that the standard library is passed in log , there are many choices.
    • Respect stdout andstderr
    • Do not usesingleton

interfaceVS struct #

Accepted interface , but the return wasstruct

This is different from Java, where Java prefers to interface manipulate everything. And Golang do not need, Golang use is recessive interface .

When do panic #

It's best not to panic . If not panic , perhaps the most appropriate place is init when, because just a run can see hanging, easier to deal with. But even so, try not to panic .

Check for error #

Q: Do we need to check all of them error ? For example, some seem to be less prone to error.
Answer: Need, especially you say these are not easy to make mistakes!!

We used it error instead exception , so don't ignore it.

Methods of Treatment

    • The best way to do this is to Bubble up, which means returning the caller
    • But sometimes (such as go routine) is not suitable, that is:
      • Make a log
      • or add a counter

When should I return an error more appropriate?

    • When the convention is not met
    • When the answer you need is not available

Allow the ability to enable library debugging #

Design for testing #

    • To make it easy for you to test
    • To facilitate library user testing

Concurrent

Channels #

Although the channel is Golang a good thing to deal with concurrency, it is not required on all occasions. For example, the standard library is rarely used in the API channel .

    • Move the position you are using channel to the upper layer.
    • You can use a callback function.
    • Do not mix with mutex andchannel

When does it start goroutine #

    • There are some libraries that New() will initiate them goroutine , which is not good.
    • The standard library uses a Serve() function. and the corresponding Close() function
    • Will be goroutine pushed to the top

When to use the context. Context #

    • All blocking, long-time operations, should becancel
    • Because context.Context it's easy to store things, it's easy to be abused. Do your best to avoid usingContext
    • SingletonAnd context.Value() something of the same nature, like a global variable, is a black box for the state of the program.

Other Precautions #

    • If something is hard to do, well, let someone else do it.
    • Upgrade for efficiency
      • however , correctness is more important than efficiency, and on the premise of correctness, pay attention to efficiency
    • Do not use in libraries /vendor ( main can be in a package)
    • Note build tag
    • Keep it clean.
      • Try to use all the static analysis tools to check the code.
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.