Calling go functions from command line

928 views
Skip to first unread message

christ...@google.com

unread,
Jul 21, 2016, 3:14:04 PM7/21/16
to golang-nuts
Cgo enables us to call C functions from Go programs, and we can run C functions from command line.

Are we able to call Go functions directly from command line?

For example if I have a function in Go:

func add(a, b int) int {
  return a + b
}

Can I somehow invoke this from bash?



Thanks!

Rick

unread,
Jul 21, 2016, 3:59:20 PM7/21/16
to golang-nuts, christ...@google.com
See os.Exec. Also there is a nice project https://github.com/codeskyblue/go-sh that friendly's the stdlib up.

carlfn...@gmail.com

unread,
Jul 21, 2016, 5:07:01 PM7/21/16
to golang-nuts, christ...@google.com
You can add the functions to a map and use the string representation passed in from the command line to execute them

var funcs map[string]func()

func add(){
  ... get parameters from command line
}

func main(){
  func, ok := funcs[funcName]
  ... test ok

  func()
}

On Thursday, 21 July 2016 12:14:04 UTC-7, christ...@google.com wrote:

Jason E. Aten

unread,
Jul 27, 2016, 6:58:00 AM7/27/16
to golang-nuts, christ...@google.com


On Thursday, July 21, 2016 at 2:14:04 PM UTC-5, christ...@google.com wrote:
...and we can run C functions from command line.

Not that I'm aware of. It's not clear what you are thinking of here.
 
Are we able to call Go functions directly from command line?

If you compile in https://github.com/glycerine/zygomys, the zygo repl will let you make calls via reflection into natively compiled Go functions.


Reply all
Reply to author
Forward
0 new messages