In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to use Go to write command-line tools". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
But at this stage, relatively speaking, Python writes a little more, and occasionally I have to go back to the furnace to write some Java; naturally, I am not familiar with Go.
So I used the weekend to do a small project to deepen some experience. So I thought of a blog gadget I wrote using Java.
At that time, a large number of pictures were banned from the Weibo picture bed, so that many pictures in personal blogs could not be viewed. This tool can back up the pictures in the article locally and replace the pictures directly to other picture beds.
Personally, I have been using it now, usually using tools such as iPic to upload pictures to the Weibo picture bed when using codewords (mainly convenient + free). After writing, use this tool to switch to [SM.MS] (a paid bed like http://sm.MS)), and back up the image to your local disk.
The effect of rewriting it to cli tool with Go is as follows:
What skills need to be mastered?
The reason why I chose this tool to rewrite with Go; one is that the function is relatively simple, but it can also take advantage of some of the features of Go, such as network IO, co-program synchronization and so on.
Does it feel more geek after being modified to a command-line tool at the same time?
Before we begin, let's introduce what knowledge points will be used for Javaer who are not familiar with Go:
Use and manage third-party dependency packages (go mod)
The application of the cooperative process.
Multi-platform packaging.
Let's start with the specific operation. I think even friends who haven't had much contact with Go can quickly get started with a gadget after reading it.
Use and manage third-party dependencies
For those who have not installed Go, please refer to the official website to install it yourself.
Its purpose and function are similar to pip in maven,Python in Java, but much easier to use than maven.
According to its use reference, we need to first execute go mod init under the project directory to initialize a go.mod file, of course, if you are using an IDE like GoLang, it will automatically create a directory structure for us when you create a new project, and of course it also includes the go.mod file.
In this file, we introduce the third-party packages we need:
Module btbgo 1.15require (github.com/cheggaaa/pb/v3 v3.0.5 github.com/fatih/color v1.10.0 github.com/urfave/cli/v2 v2.3.0)
I use three packages here, which are:
Pb: progress bar, which is used to output progress bars in the console.
Color: used to output text in different colors on the console.
Cli: command line tool development kit.
Import ("btb/constants"btb/service"github.com/urfave/cli/v2"log"os") func main () {var model string downloadPath: = constants.DownloadPath markdownPath: = constants.MarkdownPath app: = & cli.App {Flags: [] cli.Flag { & cli.StringFlag {Name: "model" Usage: "operating mode R:replace, b:backup ", DefaultText:" b ", Aliases: [] string {" m "}, Required: true, Destination: & model,} & cli.StringFlag {Name: "download-path", Usage: "The path where the image is stored", Aliases: [] string {"dp"}, Destination: & downloadPath Required: true, Value: constants.DownloadPath,}, cli.StringFlag {Name: "markdown-path" Usage: "The path where the markdown file is stored", Aliases: [] string {"mp"}, Destination: & markdownPath, Required: true, Value: constants.MarkdownPath },}, Action: func (c * cli.Context) error {service.DownLoadPic (markdownPath, downloadPath) return nil}, Name: "btb", Usage: "Help you backup and replace your blog's images" } err: = app.Run (os.Args) if err! = nil {log.Fatal (err)}}
The code is very simple, nothing more than using the api provided by cli to create a few commands to map the user input-dp,-mp parameters to downloadPath, markdownPath variables.
Then use these two data to scan all the pictures and download them to the corresponding directory.
For more instructions, please refer directly to the official documentation.
You can see that some of the syntax is completely different from Java, such as:
When declaring a variable, the type is placed after, and the variable name is defined first; the method parameters are similar.
Type derivation, variable type can not be specified (the new version of Java also supports it)
Method supports returning multiple values at the same time, which is very useful.
Public and private functions are distinguished by the case of their initials.
There are others not to be enumerated.
Cooperative process
Then the command executor invokes the service.DownLoadPic (markdownPath, downloadPath) processing business logic.
The code included here, such as file scanning and image download, will not be analyzed; the official SDK is very clear and relatively simple.
Let's focus on the goroutime in Go, that is, Synergetics.
The scenario I use here is to use a co-program to parse and download images for each file scanned, so as to improve the overall efficiency.
Func DownLoadPic (markdownPath, downloadPath string) {wg: = sync.WaitGroup {} allFile, err: = util.GetAllFile (markdownPath) wg.Add (len (* allFile)) if err! = nil {log.Fatal (read file error)} for _, filePath: = range * allFile {go func (filePath string) {allLine Err: = util.ReadFileLine (filePath) if err! = nil {log.Fatal (err)} availableImgs: = util.MatchAvailableImg (allLine) bar: = pb.ProgressBarTemplate (constants.PbTmpl) .start (len (* availableImgs)) Bar.Set ("fileName" FilePath). SetWidth for _, url: = range * availableImgs {if err! = nil {log.Fatal (err)} err: = util.DownloadFile (url, * genFullFileName (downloadPath, filePath) & url) if err! = nil {log.Fatal (err)} bar.Increment ()} bar.Finish ( ) wg.Done ()} (filePath)} wg.Wait () color.Green ("Successful handling of [% v] files.\ n" Len (* allFile) if err! = nil {log.Fatal (err)}}
Whether the code usage level looks much simpler than Java, we do not need to maintain an executorService like Java, nor do we need to consider the size of the thread pool, everything is left to Go to schedule itself.
When you use it, you only need to add the go keyword before calling the function, but here it is an anonymous function.
And because goroutime is very lightweight and takes up very little memory compared to thread in Java, we don't need precise control over the amount of creation.
But something very similar to Java is also used here: WaitGroup.
Its usage and function are very similar to CountDownLatch in Java; it is mainly used to wait for all the goroutime execution to be finished, and here it is natural to wait for all the pictures to be downloaded and then exit the program.
There are three main steps to use:
Number of goruntime created and initialized: wg.Add (len (number))
Every time a goruntime finishes executing, call wg.Done () to subtract the count by one.
Finally, the number of calls to wg.Wait () waiting for WaitGroup is reduced to 0.
For collaborative Go, it is recommended to use chanel to communicate with each other, which will be discussed later.
Packing
There is only so much core logic, so let's talk about packaging and running; this is quite different from Java.
As we all know, Java has a famous saying: write once run anywhere
This is because with the JVM virtual machine, we only need to pack out a package no matter which platform the code ends up running on; but how does Go run on each platform without a virtual machine?
To put it simply, Go can package different binaries for different platforms, which contains all the dependencies needed to run, and does not even need to install the Go environment on the target platform.
Although Java only needs to pack a package in the end, a compatible Java runtime environment has to be installed on each platform.
I wrote a Makefile here to perform packaging: make release
# Binary nameBINARY=btbGOBUILD=go build-ldflags "- s-w"-o ${BINARY} GOCLEAN=go cleanRMTARGZ=rm-rf * .gzVERSION=0.0.1release: # Clean $(GOCLEAN) $(RMTARGZ) # Build for mac CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GOBUILD) tar czvf ${BINARY}-mac64-$ {VERSION} .tar.gz. / ${BINARY} # Build for arm $(GOCLEAN) CGO_ ENABLED=0 GOOS=linux GOARCH=arm64 $(GOBUILD) tar czvf ${BINARY}-arm64-$ {VERSION} .tar.gz. / ${BINARY} # Build for linux $(GOCLEAN) CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) tar czvf ${BINARY}-linux64-$ {VERSION} .tar.gz. / ${BINARY} # Build for win $(GOCLEAN) CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GOBUILD). Exe Tar czvf ${BINARY}-win64-$ {VERSION} .tar.gz. / ${BINARY} .exe $(GOCLEAN)
You can see that we only need to specify the system variable before go build to package packages for different platforms, such as packaging files for the arm64 architecture of the Linux system:
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build main.go-o btb
You can run the program directly on the target platform. / btb.
That's all for "how to use Go to write command line tools". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.