Go, like other programming languages, offers many standard libraries to help users handler files by their buffer, position, or line. However, unlike Javascript or Node.js, Go doesn’t have any standard asynchronous input-output (I/O) libs. To get around this obstacle, you can easily use Go’s concurrency to implement non-blocking I/O. As a beginner with this language, I’ll try to list some methods for finding and catching wanted information in files using Go.

After this, strings.Split() can help to convert loaded content to a string array and regexp.MatchString() will match the regex expression with each element of the array. Be careful: you should not read a file with the ioutil.ReadFile()method if the file is too large to load in memory at once. You can learn why from the Go lib’s source code.

In this snippet, MaxScanTokenSize is the max size for each line, but it is only reading one line at time. Only if you line size is above MaxScanTokenSize, you can read the file as big as you want. If you don’t want the file to be read line by line, you can change to file.Read() and IO.copy(). These APIs allow you to define a buffer size for reading a file.

Still, if you want to use Go concurrency to help you make the job run faster, you can use the Go channel and goroutines.

This code creates 10 goroutines for the grep information job. When every goroutine is finished, the complete channel with get all true values, then any blocking code <-complete can pass.

Bechmark

The Go Testing Suite (testing) not only supports automated testing of Go packages, but it also contains some benchmark tools. All you need to do is write your test case, run the command, turn on the -benchmem flag, and add the result with memory consumption.

go test -bench=. -benchmem

When I ran the test, I used a small file, so the results and bench directory in my repo are merely showing how to benchmark a function. Here are the results I collected from my computing:

Conclusion

I am glad to know that Go supports a rather convenient and fast lib for doing some concurrency work, which can help developers break through difficult technical bottlenecks when building apps with other languages. On top of that, the performance of Go’s tools and framework, such as Gin and NSQ really impress me.

If you want to dig more into Go, I think Gopher Academy is a great place to start and you can chat with and meet more Go gophers in their Slack channel. The annual conference, Gophercon, took place last month, so look out for updates from that and look for announcements about next year’s.

I hope you enjoy exploring this cool language as much as I do. It really helped us a lot with the Myanmar project. If you have any thoughts to share about Golang or its related tools, send us an email ([email protected]) or ping us on Twitter (@wiredcraft).