What's the appropriate Go shebang line?

后端 未结 6 753
情书的邮戳
情书的邮戳 2021-02-01 01:55

I like using shebangs to run my Perl scripts directly:

#!/usr/bin/env perl

What\'s the shebang for Go programs?

6条回答
  •  北海茫月
    2021-02-01 02:19

    So far, chaining sh and gorun has proven to be the most portable solution for me.

    ///bin/sh -c true && exec gorun "$0" "$@"
    
    package main
    
    import (
        "fmt"
        "log"
        "os"
    )
    
    func main() {
        fmt.Println("hello")
        log.Printf("args: %v", os.Args)
    
        // just to test exit code, would be 0x11
        os.Exit(17)
    }
    
    

    OUTPUT:

    00:~ $ chmod a+x test.go && ./test.go rawr
    hello
    2020/01/21 23:17:40 args: [./test.go rawr]
    11:~ $ 
    

提交回复
热议问题