This is a creation in Article, where the information may have evolved or changed. Go run in the second chapter of Go concurrent programming, I introduce the classification of Go source files. Go source files include: command source files, library source code files and test source files. Where the command source file should always belong to the main code package, and in which there is no parameter declaration, no result declaration of the main function. A single command source file can be compiled separately or individually (you may need to set the environment variable Gobin). Of course, command source files can also be run separately. We want to run the command source file to use the command go run. The Go Run command compiles and runs the command source file. Because it contains a compilation action, it can also accept all tags available for the Go Build command. In addition to the tag, the Go Run command only accepts the go source file as a parameter and does not accept the code package. Like the Go Build command and the Go Install command, the Go Run command does not allow multiple command source files to be used as parameters, even if they are in the same code package. And the reason is also consistent, multiple command source files will have a main function declaration. If the command source file can accept parameters, then when you run it using the Go Run command, you can put its parameters behind its file name, like this: hc@ubt:~/golang/goc2p/src/helper/ds$ go run showds.go-p ~/ GOLANG/GOC2P in the above example, we use the Go Run command to run the command source file Showds.go. This command source file can accept a name called "p" parameters. We use the form "-p" to indicate that "P" is a parameter name and not a parameter value. It needs to be separated from the source file name by a space. The parameter value is placed after the parameter name, and both pairs appear. They must also be separated by a space. If there is a second argument, a space is also needed between the parameter name of the second parameter and the parameter value of the first parameter. And so on The Go Run command can only accept one command source file and several library source files (must belong to the main package) as the file parameters, and can not accept the test source files. It checks the type of the source file when it executes. If there are multiple or no command source files in the parameter, then the Go Run command will only print the error message and exit without continuing. After passing the parameter check, the Go Run command will compile the command source file in the parameters and store the compiled executable file in the temporary working directory. Compiling and running the procedure in order to more intuitively reflect the steps in the Go Run command, we add the tag-n when executing the command, which is used to print the relevant command without actually executing it. Now let's simulate the code package that runs the GOC2P project helper/ds command source file showds.go. Examples are as follows: hc@ubt:~/golang/goc2p/src/helper/ds$ go run-n showds.go## command-line-arguments#mkdir-p $WORK/ Command-line-arguments/_obj/mkdir-p $WORK/command-line-arguments/_obj/exe/cd/home/hc/golang/goc2p/src/helper/ds /usr/local/go1.5/pkg/tool/linux_amd64/compile-o $WORK/command-line-arguments.a-trimpath $WORK-P Main-complete- Buildid df49387da030ad0d3bebef3f046d4013f8cb08d3-d _/home/hc/golang/goc2p/src/helper/ds-i $WORK-pack./showds.gocd ./usr/local/go1.5/pkg/tool/linux_amd64/link-o $WORK/command-line-arguments/_obj/exe/showds-l $WORK-W-extld=clang -buildmode=exe-buildid=df49387da030ad0d3bebef3f046d4013f8cb08d3 $WORK/command-line-arguments.a$work/ Command-line-arguments/_obj/exe/showds in the example above does not show the relevant print information for compiling and running a dependent package for the command source file Showds.go. This is because all the dependencies of the source file have been compiled before. Now, let's explain the printed information line by row. The prefix "#" begins with the comment information. We see three lines of comment information in the message, and the content "Command-line-arguments" appears in the middle row. As we said in the Go Build command, when the compile command parses the parameter, if it finds out that the first parameter is a go source file rather than a code package, a virtual code package called "Command-line-arguments" is generated internally. So the message here is to tell meThe following lines of information are about the virtual code package "Command-line-arguments". The "$WORK" in the print information represents the absolute path to the temporary working directory. To store the results of the compilation of the virtual code package "Command-line-arguments", the command creates a subdirectory named Command-line-arguments in the temporary working directory, under which _obj subdirectories and _obj/exe subdirectories are created. Then, the command program uses the Go Language tool directory compile command to compile the command source file Showds.go, and the result file is stored in the $work directory, named COMMAND-LINE-ARGUMENTS.A. Among them, compile is a programming tool that comes with the go language. After the compilation succeeds, the command program uses the link command to link to generate the final executable file and save it in the $work/command-line-arguments/_obj/exe/directory. The last line in the print message indicates that the command ran the resulting executable file. By interpreting these printed commands, we understand the purpose and content of the temporary working directory. In the example above, we just let the Go Run command print out the commands that need to be executed in the process of running the command source file Showds.go, without actually running it. If we want to really run the command source file Showds.go and want to know the location of the temporary working directory, we need to remove the tag-N and Mark-work. Of course, if you still want to see the commands executed during the process, you can add tag-X. If the reader has already seen our introduction to the Go Build command, we should know that tag-X, like tag-N, will print out the process execution commands, but the different commands will be executed. The commands after adjusting these tags are like this: hc@ubt:~/golang/goc2p/src/helper/ds$ go run-x-work showds.go when the command is actually executed, there will be real content in the temporary working directory, like this:/tmp /go-build604555989:command-line-arguments/_obj/exe/showds COMMAND-LINE-ARGUMENTS.A because the above command contains the-work tag, we can find the actual working directory from its output (this is/tmp/go-build604555989). Interestingly, we can just run the command source file Showds.go to see the directory tree for this temporary working directory: hc@ubt:~/golang/goc2p/src/helper/ds$ Go run showds.go-p/tmp/go-build604555989 readers can try them on their own. As we have described earlier, the command source file if you can accept parameters, you can run the Go Run command the command source file when the parameter name and the value of the argument is appended in pairs. In fact, if a parameter is appended to the command, a consistent argument is appended to the last execution of the generated executable file. For example, if you execute a command like this: hc@ubt:~/golang/goc2p/src/helper/ds$ go run-n showds.go-p ~/golang/goc2p the last command printed with the-X or-n flag is: $ Work/command-line-arguments/_obj/exe/showds-p/home/hc/golang/goc2p can be seen, the Go Run command appends the parameters appended to the command source file to the corresponding executable file intact. The above briefly shows a command source file from compile to run the whole process. Keep in mind that the Go Run command contains two actions: compiling the command source file and running the corresponding executable file. 204 reads
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.