-1

Is there an equivalent in golang to check if a function exists in a package/ go file like it is done in python.

mymethod in dir(package)
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
pretty08
  • 137
  • 3
  • 11
  • 1
    Compiler do this – Uvelichitel Jul 15 '18 at 18:33
  • 1
    Go does not maintain a directory of functions runtime. What is the higher-level problem that you are trying to solve? – Charlie Tumahai Jul 15 '18 at 19:08
  • Im trying to parse and flatten a json file, based on the type of flattening method passed as an argument to the main method, I need to check if the flatten method exists in my go package and then call it if it exists. – pretty08 Jul 15 '18 at 19:14
  • Im trying to parse and flatten a json file, based on the argument passed in my main method, I need to check if a function exists as of the argument passed, in my go package and then call it if it exists. – pretty08 Jul 15 '18 at 19:31
  • 2
    Typical approach is to specify the function using a function value (so there's no need to look it up, just call it) or to look up the function in an application maintained map. It will help if you show the Go code you have attempted so far. – Charlie Tumahai Jul 15 '18 at 19:39

2 Answers2

1

It looks like from this post that it is not directly possible.

You could always create your own package to search package files and check if the method exists, as explained in the above post's first answer.

jonroethke
  • 1,152
  • 2
  • 8
  • 16
  • But that requires access to the source code at runtime, which is not the usual case. – Adrian Jul 16 '18 at 15:06
  • @Adrian True, but the same would need to be the case using Python. I am [probably incorrectly] assuming we have access to the source code and not just a compiled package. – jonroethke Jul 16 '18 at 15:57
  • That is always the case in python, it's an interpreted language. The source is what gets executed. That's not true of Go. – Adrian Jul 16 '18 at 16:28
1

Based on your comments, it sounds like you may simply use a map the contains available functions. Here's a complete example:

package main

import (
    "flag"
    "fmt"
    "os"
)

func main() {
    var option string
    var data string

    flag.StringVar(&option, "option", "default", "The option to choose from")
    flag.StringVar(&data, "data", "", "The data to use")

    flag.Parse()

    options := map[string]func(string) string{
        "foo": func(data string) string {
            fmt.Println("do something with data using option foo")
            return data
        },
        "bar": func(data string) string {
            fmt.Println("do something with data using option bar")
            return data
        },
        "default": func(data string) string {
            fmt.Println("do something with data using default option")
            return data
        },
    }

    method, found := options[option]
    if !found {
        fmt.Println("option is not available")
        os.Exit(1)
    }

    result := method(data)
    fmt.Println(result)
}

Output

$ go run main.go -data hello -option foo
do something with data using option foo
hello

$ go run main.go -data hello -option whatever
option is not available

$ go run main.go -data hello
do something with data using default option
hello
Miguel Mota
  • 20,135
  • 5
  • 45
  • 64