This is a creation in Article, where the information may have evolved or changed.
Must read before reading, all of the following is from here. The purpose of this place is to be more than a pair of readers. In all fairness, the same markdown, blog Park layout really x look, how to see how x look. (X: = ' Difficult ' | | X: = ' resistant ' | | X: = ' good ') are original, not plagiarism. If really want to take, also so-called, at least give me a name, also not wasted code word of bitterness. Did you say that?
Talk to the last section and keep talking. If you see "Hello God, My golang," then check if you have found an exciting piece of music and have a good cup of tea next to it. If so, turn off the music and drink the tea. Writing code needs to be all-in-one, with music next to the ear, so are you still listening to music? Or do you keep the logic? So it's best to stop the music and concentrate. If you keep dropping ... Then don't stop. Write code The mood is the most important, no matter what, happy is good. Just get up and your code will fly!
Gentlemen, ladies and gentlemen. If there is still time to ditch the pit, as the following is about to start your real Golang occupy the pit, yuck, Golang into the pit trip.
Golang's Library
There are standard libraries and third-party libraries in the case of a slightly more well-known programming language. In layman's terms, the standard library is an official library that is maintained by the authorities. Others can only see can not change, with a bad mood can only be uncomfortable. If one day is really uncomfortable, write a library to replace the official library, the library is a third-party library.
For Golang, the official library and third-party libraries are very easy to set up. The library name of the official library is short and powerful, for example: Fmt,net/http,log, etc. these very short library names are the official library. Corresponding to a third-party library, long and not easy to remember, for example: Github.com/julienschmidt/httproute (a lightweight network routing library), Github.com/andy-zhangtao/gogather/random ( Self-encapsulated tool library).
In addition to the above two types of libraries, there is a special class of libraries that start with golang.org, such as Golang.org/x/build. This kind of library is not used frequently, so it is not listed in the standard library, but it is golang official maintenance, so it is equivalent to the second standard library.
The above three classes of libraries, when used in preference to use the standard library. If the standard library does not meet the requirements, then use the second standard library (the library that starts with golang.org/), or it will not meet your needs, then choose to use a third-party class library. Looking for a third-party library, go to GitHub to find out. But in the boundless crowd, you have me. (The person who can hum the tune will be exposed to age.)
Oh, open a little joke, adjust the atmosphere.
GitHub above the code too much, a mess to find, the effect is not good. So first Google search, such as you want to process JSON, search: Golang json. If you are creating a RESTful API, search Golang create restful API. Take a look at which library the person who has been recommending, and then shop around, the star on GitHub, is a dead-powder fan and uses it directly.
Use? How to use it? Look down!
Golang's Tools
After you install the Golang, the standard library is already installed. How to install the following two types of libraries? First of all, do not expect the two types of libraries are completely installed, Golang installation package can not be "fully installed" this option. The latter two classes are installed on demand and then installed when you need them. For example, at this point, you need to write a Web server, the standard library net/http can easily create a webserver, but we want to handle the RESTful API style requests, the standard library is awkward and useless. At this point, we install a third-party library, such as Github.com/julienschmidt/httprouter. The installation process is very simple, hands-on, follow me after knocking the letter below, do not lazy oh:
go get github.com/julienschmidt/httprouter
No error, is the installation success. Where did you put it? Old Tiecai a guess? Guess the right prize Oh ~
No clue? Go back to the previous section and look for four words: Workspace (gopath). All the source code is stored in the gopath/src inside, so oneself to gopath/src/github.com/julienschmidt/httprouter inside to see, is not full is. Go source?
Now all you need to do is remember this one tool go get on the line. Or that sentence, steamed bun to take a bite to eat, porridge to a mouthful of drink, find why anxious ah, slowly.
When you need a non-standard library, you will be adept at typing go get < library name > dashing into a return, and then can not pretend to see the success of the good fortune it (because GFW exist, so you know ...) ) Good luck, one go. Bad luck, hehe, go out stuffy root smoke, kan days, come back in to see if the luck value is bursting.
What if the library is upgraded? Sir, do not think that the library is immutable. Marx said that all things in the world are changing. When the third-party library also changed, in the skilful knock a go get-u < library name, and dashing to knock a carriage return, and then can be installed to see the success of the good fortune it (because of the existence of GFW, so you know ...) ) Good luck, one go. Bad luck, hehe, go out stuffy root smoke, kan days, come back in to see if the luck value is not bursting (here in order to show the content of enrichment, directly copied the last paragraph ️).
See here, let's have a little refresher. Here are three questions to look at you know not (this picture is on my GitHub, see not complete on refresh and try again):
The first Golang Library of your Life
Life is not tired, absolutely suffer. People, born just to blind toss. Do not toss the life, is either a vegetable, or is dying of people. The same is true of code, which is not practiced by light. In this little chapter, we begin to create the first Golang library in our lives.
Because it is a third-party library, a name called Github.com/user/stringutil, the middle user can be changed to your GitHub account name. Take a good name and build a directory. Let's create this library:
mkdir $GOPATH/src/github.com/user/stringutil
Next, create a file named in this directory reverse.go
, with the following content:
// stringutil 包含有用于处理字符串的工具函数。package stringutil// Reverse 将其实参字符串以符文为单位左右反转。func Reverse(s string) string { r := []rune(s) for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 { r[i], r[j] = r[j], r[i] } return string(r)}
The expert knocks personal overdraft knocks, really cannot hand knocks on duplicates. But out of the mix, sooner or later to debts, write code especially so. After the hand-tap/paste is complete, use the go build
command to test the package's compilation. In the $gopath/src/github.com/user/stringutil directory, do not run the wrong directory.
Under normal circumstances, should not be an error. If the error, is the wrong letter/paste line, and then look back and check. Now your life's first Golang library has been created, do not doubt yourself, do not doubt me, really! Your first Golang library was created.
I know your question, how do I use the This library? (How do I use this library?) ) down the mouse.
Create a $gopath/src/github.com/user/hello directory.
mkdir $GOPATH/src/github.com/user/hello
Create a main.go inside, then hand-tap or paste the following code:
package mainimport ( "fmt" "github.com/user/stringutil")func main() { fmt.Printf(stringutil.Reverse("!oG ,olleH"))}
Executes go build. See if it will come out a hello binary file that executes this binary file
./helloHello, Go!
If the error, then check your directory structure is not like this:
src/ github.com/user/ hello/ hello.go # 命令源码 stringutil/ reverse.go # 包源码
If not, adjust it. If so, see if you've pasted the wrong or wrong code.
This chapter is the first pit of your Golang journey. Okay, not deep. only to the ankle. Take a deep breath, don't spit it out, and slowly experience the feeling of being suffocated ... The back of the Golang trip will also have a pit, I will go from the inside out, from easy to difficult, slowly said. So do not worry, into the back into the door, we will fly faster.