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