A little detail of go run

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

One of the advantages of Golang is that the compilation is fast and can be used as a scripting language, using go run to execute the. Go file directly, just like executing a script.

I like this very much, and I don't usually bother to build. However, when trying to Beego (Https://github.com/astaxie/beego) frame, just write a Hello world, always wrong, prompted to find the template.

Start a long debugging, look at the source code, modify the configuration 、、、 a variety of not.

2013/05/01 10:03:11 [C] [Handler crashed with error can ' t find templatefile in the PATH:INDEX.TPL]
2013/05/01 10:03:11 [C] [/build/buildd/golang-1/src/pkg/runtime/proc.c 1443]
2013/05/01 10:03:11 [C] [/usr/lib/go/src/pkg/github.com/astaxie/beego/controller.go 178]
2013/05/01 10:03:11 [C] [/usr/lib/go/src/pkg/github.com/astaxie/beego/controller.go]
2013/05/01 10:03:11 [C] [/usr/lib/go/src/pkg/reflect/value.go 521]
2013/05/01 10:03:11 [C] [/usr/lib/go/src/pkg/reflect/value.go 334]
2013/05/01 10:03:11 [C] [/usr/lib/go/src/pkg/github.com/astaxie/beego/router.go 375]
2013/05/01 10:03:11 [C] [/usr/lib/go/src/pkg/net/http/server.go 656]
2013/05/01 10:03:11 [C] [/build/buildd/golang-1/src/pkg/runtime/proc.c 271]

Then debug it:

Go Build Main.go

GdB./main

Run

It's all right.

Direct execution./main, it's OK.

Read the relevant code again, Beego find the configuration, template and other files are from the current execution directory to find relevant subdirectories.

Beego.go 48:apppath, _ = os. GETWD ()

Suspect that the execution path of Go run is not the current path.

Check out the Help for Go run:

Go help Run
Usage:go run [build flags] gofiles ... [Arguments ...]
Run compiles and runs the main package comprising the named Go source files.
For more on build flags, see ' Go help build '.
See Also:go build.

See this meaning, is automatically compiled + run ...

The first thing to suspect is that this path is not the path to which the Go tool is located.

Verify:

Go run./main.go

PS, hmm, where's a.out?

49391 PTS/1 00:00:00 Go
49399 PTS/1 00:00:00 a.out

Take a look at the details:

0 49391 42236 0 66416 5204 ffffff sl+ pts/1 0:00 go run./main.go
0 49399 49391 0 146000 4272 ffffff sl+ pts/1 0:00/tmp/go-build402905850/command-line-argument S/_obj/a.out

It is clear here that the Go Run command actually runs to the/tmp directory to create a/go-buildxxxx directory, compile and run.

This could bring up a minor problem,

1. If you depend on the relative path of the file, like above, there will be problems;

2. If executed continuously, new temporary files will be generated continuously.

So Golang should be careful when scripting.

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.