3 ms·
I'm a fan of Go's "go run". For example, you can do: go run main.go or: go run ./cmd/server When invoked this way, Go compiles your code behind the scen
by atombender 7y ago
I'm a fan of Go's "go run". For example, you can do:
go run main.go
or:
go run ./cmd/server
When invoked this way, Go compiles your code behind the scenes, but you don't have to create a binary.
This means Go lends itself pretty well to ad-hoc scripting or writing small script-like tools.
Unfortunately, Go doesn't support shebang lines, but with gorun [1] you can:
#!/usr/bin/env gorun
package main
func main() {
println("Hello world!")
}
[1] https://github.com/erning/gorun https://github.com/erning/gorun
- floatboth 7y agoD compilers do work as a shebang already.
- paulddraper 7y agoD already works like this. Scala (Ammonite) works like this. The author isn't complaining about keystrokes; he wants the compile + run process to be faster. Using an interpreter instead of a compiler would make it faster.
- apta 7y agoJava has the same now (since Java 9 I think).
- btbytes 7y agoD's default package manager already allows you to write code like this which 1. runs the code like a script 2. downloads dependencies if they are required. #!/usr/bin/env dub /+ dub.sdl: name "allthepythons" dependency "d-glob" version="~>0.3.0" +/ import std.stdio : stdout; import glob : glob; void main () { foreach (entry ; glob("/usr/local/bin/python*")) { stdout.writefln("%s", entry); } }
- 1980phipsi 7y agoDub's documentation should use some love...
- grogenaut 7y agoI actually use this for all of my complicated build scripts and just use make to glue them together. For instance I have a stack name from computer,branch,sha,username that I calculate so I have a different stack per thing I'm working on. I tried getting that to work with awk fu but doesn't work the same on every platform, so instead "go run cmd/get_stack_name/main.go" instead. Fast and like it.