N
N
NOONE2018-10-02 14:46:40
go
NOONE, 2018-10-02 14:46:40

Golang function trigger via command cli?

Hello, tell me please, I can’t find anything on the topic, there is a code

package main

import (
  "encoding/json"
  "flag"
  "fmt"
}
func main() {
  textPtr := flag.String("")
        check()
  }
func check() { 
  
функция что-то делает и ничего не возвращает
}

The question is how to execute the check function by calling the binary from the command line with an argument, for example /couch.go --check
Thank you, I have never worked with the command line in GO before

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2018-10-02
@Djam36

As far as I understand, there is a simple solution (for a limited number of called functions) and a complex one (with dynamic function calls).
Simple solution (on the forehead):

import "os"

func main() {
    if len(os.Args) > 1 {
        if os.Args[1] == "--check" {
            check()
        }
    }
}

It is also obvious that it is possible to parse the parameters in a more "correct" way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question