douqin0676 2019-01-10 06:35
浏览 12

如何在main.go中找到运行功能的条目?

i am not a GO programmer, when i read the code of GO,i find such code

func main() {
  ......
        run(options)
}

and i am very confused the function run will run what? anyone can help?

  • 写回答

2条回答 默认 最新

  • duanlu6268 2019-01-10 06:46
    关注

    Well, to be fair, the code you posted will produce the following:

    prog.go:4:3: syntax error: unexpected ..., expecting }
    

    https://play.golang.org/p/HMv-FydjKWf

    However, in a more complete example:

    package main
    
    import "fmt"
    
    type Options struct {
        Enabled bool
    }
    
    func run(opts Options) {
        fmt.Printf("%+v
    ", opts)
    }
    
    func main() {
        opts := Options{
            Enabled: true,
        }
    
        run(opts)
    }
    

    https://play.golang.org/p/-bHPWWxi-wm

    The basics of what's occurring, is Go starts the execution of your program from the main func. In this case, you're calling the function run, and providing it some runtime options that you decided (such as my case, of Enabled being true for no reason).

    I suggest taking a look at https://tour.golang.org/ to get acquainted with the language. Nothing particularly unique is happening in Go, minus a few syntactical choices.

    Go is very obvious with its approach to code. To that end, nothing is magic, and there is a very simple programming explanation to what's happening. Not sure what run does? Go to that function and check it out! Once you figure out the syntax, you'll be surprised how readable things are.

    +Edit: One final point I'll make, is that if you're having difficulty just finding func run(), just "Find in directory" or whatever your editor of choice calls it. The function will be in a file in the same folder. I also recommend checking out some Go packages for various editors (VS Code, Sublime, Atom, whatever), so you can just click-through to the location of the function. These little time savers help, and basically just "Find in directory" for you.

    评论

报告相同问题?