The paper come zhongjue shallow, and must know this to practice.

These two days, I saw the GO essay contest of the Nuggets. Looking at the small book exchange code, I couldn’t help but move, but then I thought, I haven’t contacted GO at all. How to write a good essay contest?

Thinking of this, I shook my head, how can the man say no, since I never know GO, I now learn not on the line, the feeling after learning and summary write down, is not more suitable for the theme of this essay?

Technical topics for reference: The Golang Primer series

I just opened baidu search go and browsed the first page, found two entry-level tutorials and the official website, and started my Golang entry journey.

1. Install

Golangๅฎ˜็ฝ‘ xiaobai learning the first step, install the environment.

First of all, I opened the official website and found the download link of Windows environment installation package with my English level in junior high school. Just like all languages nowadays, I double-click -> Install -> Next Step -> Finish. Installation is as easy as drinking water.

With Java installation experience, I was skilled in opening the DOS window and entering: Go version and go env commands respectively for testing.

I smiled with relief when I saw that both commands produced the desired results. Install, that’s all.

However, I noticed that all the tutorials on the Internet asked me to set the go-Path after installation. It was a familiar feeling, as if I had gone back to the afternoon when I first learned Java four years ago. The first step after installing JDK was to set the Java-Path.

As a new language, GO can’t have that historical baggage, so I clicked on the computer’s environment variables with sadness:

Sure enough, the system has helped me set up, I do not have to manually set up.

But call the directory I had did not go folder, it didn’t help me to create, and according to the regulation of go, every project you specify here the environment variable at a time, that is to say, if there are ten items on a computer, you will be on the go – PATH behind writing ten projects folder address, no free ~ ~ ~ ~

Anyway, now that the environment variable is ready, we can write our hello-world, which is the first thing to do.

2. Hello-World

Golang’s official website has a code that tells you how to print Hello World using GO. I’ll COPY it down and paste it.

But on second thought, according to GO, there must be bin, PKG, SRC directories under the GO project folder. I directly used the default GO folder as the root directory of the project, so I manually created these three folders under here.

Once built, I opened the universal editor -VSCODE, downloaded a go language support and started writing.

When I’m ready to write, VSCODE reminds me to install a bunch of plugins:

From the name of this pile of things, it should be a toolkit related to Go, but because I failed to download it several times even from GitHub, I had better find a way online to use Go-proxy to set up the domestic proxy, but I still downloaded it from GitHub after setting up. Finally, I had to reboot VSCODE to download it slowly (again from GITHUB).

With that done, I wrote my first GO program, Hello-world, in the SRC package, created a new file called go. GO, and copied the code from the official website as follows:

package main

import "fmt"

func main(a) {
	fmt.Println("Hello, world")}Copy the code

So our program is written, followed by manual compilation and run, go provides go build and go run commands to help us compile and run.

The go build command is to compile, and the go run command is to compile and run, but there are other differences between the two, so that’s all I can remember for now.


I can talk a little bit more about this hello-world code, package main declares that this is the entry class for the main function, import “FMT” means that we use the go built-in FMT package, and finally we define a function called main that prints the string using Println from the FMT package.

If you have a foundation in another language, this is too small.

3. Stumbling across a roadblock

Next I am ready to rely on their own a little Java foundation, directly write a reptile to practice hand, I see everyone is climbing douban film practice hand, I will climb douban reading TOP250 bar.

I haven’t seen anything about GO before, just how to define variables, which is a bit counterintuitive, because GO’s variable definition puts the type after it. Let me show you:

var str string
Copy the code

When I first saw it, I did not like it at all, because it went against my habit. However, when I actually wrote it, I found that it was really not the case. Go is a bit like JS, and its type can be inferred directly.

str := "And great ears!"
Copy the code

We can usually write it this simple way, which means that variable assignment is two in one.

When I write crawlers later, I basically define variables in this way, so I don’t have to specify the type myself.

Syntax is not a problem, the first problem I encountered was the guide package, because to write crawlers, I need at least a DOM tree parsing tool, otherwise I will be tired to write regular.

At first, I was laughing at GO for putting projects under GOPATH. Later, I looked up information on the package guide issue and found that since GO introduced the mod module, projects can be placed in any folder.

Mod module in my understanding is a package manager, the usual project needs to use the package are downloaded, and then the project used directly guide, if not to remote download, similar to the Java Maven and Gradle concept.

Since involved in the package, the remote must be slow ah, must join the domestic mirror to get up quickly:

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
Copy the code

After joining the domestic mirror, we can happily download the packages we need:

go get github.com/PuerkitoBio/goquery
Copy the code

Goquery is similar to jquery. It is the go version of jquery and can be used to easily process HTML.

Basically, it’s just a tool for manipulating DOM trees.

With the dependencies downloaded, we can create our own project

To initialize your go project, select a folder, open DOS and type go mod init XXX. XX refers to the name of the project, and then under this folder will appear the go.mod file, which contains the dependencies introduced into the project. It’s all automatically generated. Don’t worry about it. Just focus on coding.

4. 4. Smart laundry peg

I created a hello-go folder, initialized it, and created a go.go file. After writing the main function, the body of our program was ready to write.

package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
	"strconv"
	"strings"

	"github.com/PuerkitoBio/goquery"
)

func main(a) {

	// Create the TXT file we want to export
	file, err := os.Create("Top250.txt")
	iferr ! =nil {
		fmt.Println(err)
	}
	defer file.Close()

	// Create a client object to send the request
	var client = http.Client{}

	for i := 0; i < 250; i += 25 {
		// Send a new request
		req, _ := http.NewRequest("GET"."https://book.douban.com/top250?start="+strconv.Itoa(i), nil)
		// Set user-agent must
		req.Header.Set("User-Agent"."Mozilla / 4.0 (compatible; MSIE 6.0; Windows NT 5.1)")

		// Send the request
		resp, err := client.Do(req)
		// Handle exceptions
		iferr ! =nil {
			fmt.Println("http get error", err)
			return
		}
		/ / close the flow
		defer resp.Body.Close()

		// Build the contents of the stream into a DOM tree with goQuery
		doc, err := goquery.NewDocumentFromReader(resp.Body)
		iferr ! =nil {
			log.Fatal(err)
		}

		// Get the set of all marked nodes
		// And each loop, I = serial number, s = node itself
		doc.Find("div.indent>table>tbody>tr.item").Each(func(i int, s *goquery.Selection) {
			// Get Items from the node collection
			item := s.Find("td[valign=top]")

			// Get the data we need from the Item node
			// Whitespace to wrap data
			bookName := strings.Replace(strings.Replace(item.Find("div.pl2>a").Text(), "\n"."".- 1), ""."".- 1)

			author := strings.Split(s.Find("p.pl").Text(), "/") [0]

			quote := strings.Replace(strings.Replace(s.Find("p.quote").Text(), "\n"."".- 1), ""."".- 1)

			// After we have processed the data, the next step is to fill the TXT

			//fmt.Print("TOP" + fmt.Sprint(i) + "-" + bookName + "-" + author + "-" + quote)

			// Handle characters with direct space length, align as best you can
			bookName = bookName + strings.Repeat("", (120-len(bookName)))
			author = author + strings.Repeat("", (50-len(author)))

			content := "TOP" + strconv.Itoa(i) + "\t" + bookName + author + quote + "\n"

			file.WriteString(content)
		})
	}

	fmt.Print("Program execution completed, please check the result.")}Copy the code

Friends can directly paste into their editor, package will automatically import, and then run this file.

I basically searched around for the code:

  • How does GO send requests?
  • How does go remove string whitespace?
  • How does go get the string length?
  • How does GO read and write files?

I meet mountain kaishan, meet water bridge step by step to find the knowledge point I need to use, and then put them together to a ~~~~

To show you the results:

It’s even better if you export it in Excel, and by the way, run you should just say go run go.go.

Ok, today’s essay is here, in general, GO is still very convenient to use, but the time is too short, do not have a good study of GO concurrency related knowledge ~~~

See you next time.

๐Ÿ† technology project phase ii | and I Go those things…