Golang : Go as a script or running go with shebang/hashbang style
One of the job scope of Unix/Linux system administrator is to write shell scripts to accomplish certain task. However, there are times when the shell scripting language has limited capabilities. Such as establishing secure transport layer or upload files to AWS-S3. With Golang, these can achieved easily and it is easy to use Golang to replace shell scripts.
To execute Golang program as a shell script. Put this line at the top of your code.
//usr/bin/env go run $0 $@; exit
NOTE : For Golang, the line with //
will treated as comment and this is to shut up the compiler. As for the shell executing this script, the //
line will invoke go
command with the name of the file and then exit.
For example :
//usr/bin/env go run $0 $@; exit
package main
import "fmt"
func main() {
fmt.Println("Running as a script!")
}
Next, change the permission of the file to become executable.
chmod a+x hashbang.go
and
>./hashbang.go
Running as a script!
Hope this helps!
Reference :
http://unix.stackexchange.com/questions/162531/shebang-starting-with
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+4k MariaDB/MySQL : Form select statement or search query with Chinese characters
+14.8k Golang : Read a file line by line
+14.3k Golang : Get checkbox or extract multipart form data value example
+28k Golang : JQuery AJAX post data to server and send data back to client example
+4.7k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+5.1k Golang : Extract unicode string from another unicode string example
+7.2k Golang : Reverse text lines or flip line order example
+5.1k Golang : How to verify input is rune?
+8k Golang : Generate random Chinese, Japanese, Korean and other runes
+6.3k Golang : Gorrila mux.Vars() function example
+5.2k PageSpeed : Clear or flush cache on web server
+13.3k Golang : How to shuffle elements in array or slice?