Go language Skills · Zablog

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

A word of skill

    • Throw your object-oriented brain into your home and embrace the interface. @mikegehard
    • Learn how to do things with go, and don't force other programming styles into go. @DrNic
    • The multi-use interface is always better than less. @evanphx
    • Embrace this concise, parallel, neat language. @francesc
    • It's great to read all the documents on the official website golang.org. @vbatts
    • Don't forget to use it gofmt . @darkhelmetlive
    • Read the source code more. @DrNic
    • Learn the tools and components and then create your own! Code codes and learning code are essential to success. @coreyprak
    • Learning without thinking is not the case, but thinking without learning is dangerous. Analects

Multiple ways to introduce a package

There are several unconventional ways to introduce packages. Next I'll use it fmt as an example:

    • import format "fmt"-to fmt create an alias. Replace all the used content in fmt the code format.fmt.
    • import . "fmt"-Allow content inside the package to be fmt directly referenced without prefix
    • import _ "fmt"-Prevents the compiler from prompting for the introduction of fmt content without using it, and executes the initialization function in the package. A reminder that in this case fmt is not callable

Check out this blog for more details.

Goimports

Command goimports to update your go import row, add missing rows, and delete unreferenced boot lines.

It has gofmt the same capabilities as (plug-in substitution), but goimports adds additional functionality to repair imports.

Organization

Go is a relatively easy-to-learn programming language, but for developers, the most difficult thing to do with the language at first is how to organize the code. scaffoldingis one of Rails the reasons people like it, it can give new developers clear direction, let them understand where to insert code, should follow what kind of programming style.

As an extension, go uses go fmt such tools to provide the same functionality as developers. In the same way, the go compiler is very strict and does not compile unused variables or import declarations that are not used.

Custom constructors

I often hear people ask, "When should I use a custom constructor like newjob ?" "In most cases you don't need to do that," my answer is. However, when you need to set the value at initialization and you have some default values, it is best to use a constructor. In this example, the constructor is more meaningful, so we can build a default logger with the following code:

 12345678910111213141516171819 
 package  mainimport  ( "log"   "OS" ) type  Job struct  {Command span class= "keyword" >string  *log. Logger}func  newjob   (command string )  *job   {return  &job{command, log. New (OS. Stderr, , log. Ldate)}}func  main   ()   {newjob (). Print ( "Starting now ..." )} 

Break down the code into different package

Refer to this blog to refactor the Go code, the first part is about the organization of the package.

Reference documents

    1. GO bootcamp-everything need to know-get started with go:tips and Tricks
    2. Analects
    3. Refactoring Go Code
    4. Alternate Ways of importing Packages
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.