0

I wrote a HelloWorld.py and using grumpy I compiled the HelloWorld.py to a Go source code. but after running go build, the binary file does not get generated, the go build command was executed successfully without any error but there was no binary file found in the folder.

here is the code inside HelloWorld.py:

def hello():
        print("hello, world")

here is the generated code in hello.go:

package __main__

import πg "grumpy/build/src/grumpy"

var Code *πg.Code

func init() {
    Code = πg.NewCode("<module>", "hello.py", nil, 0, func(πF *πg.Frame, _ []*πg.Object) (*πg.Object, *πg.BaseException) {
        var πR *πg.Object; _ = πR
        var πE *πg.BaseException; _ = πE
        ßhello := πg.InternStr("hello")
        var πTemp001 *πg.Object
        _ = πTemp001
        var πTemp002 []πg.Param
        _ = πTemp002
        for ; πF.State() >= 0; πF.PopCheckpoint() {
            switch πF.State() {
            case 0:
            default: panic("unexpected function state")
            }
            // line 1: def hello(): print "hello, world"
            πF.SetLineno(1)
            πTemp002 = make([]πg.Param, 0)
            πTemp001 = πg.NewFunction(πg.NewCode("hello", "hello.py", πTemp002, 0, func(πF *πg.Frame, πArgs []*πg.Object) (*πg.Object, *πg.BaseException) {
                var πTemp001 []*πg.Object
                _ = πTemp001
                var πR *πg.Object; _ = πR
                var πE *πg.BaseException; _ = πE
                for ; πF.State() >= 0; πF.PopCheckpoint() {
                    switch πF.State() {
                    case 0:
                    default: panic("unexpected function state")
                    }
                    // line 1: def hello(): print "hello, world"
                    πF.SetLineno(1)
                    πTemp001 = make([]*πg.Object, 1)
                    πTemp001[0] = πg.NewStr("hello, world").ToObject()
                    if πE = πg.Print(πF, πTemp001, true); πE != nil {
                        continue
                    }
                }
                if πE != nil {
                    πR = nil
                } else if πR == nil {
                    πR = πg.None
                }
                return πR, πE
            }), πF.Globals()).ToObject()
            if πE = πF.Globals().SetItem(πF, ßhello.ToObject(), πTemp001); πE != nil {
                continue
            }
        }
        return nil, πE
    })
    πg.RegisterModule("__main__", Code)
}

when i try to add the main function to the end of the hello.go and call init() like this :

func main() {
    init()
}

i get this error when i run go build

can't load package: package .:  main.go:1:2: expected 'package', found 'EOF'
sam
  • 335
  • 2
  • 5
  • 19
  • 2
    Where is your `main` function? – Pandemonium Feb 02 '18 at 19:37
  • Go can only build a binary from a `main` package with a `main` function. – JimB Feb 02 '18 at 19:41
  • I do not have a main function I tried to edit the generated code to have a main function but it fails to compile , how can I call the hello world function in a main function ? The code looks very unfamiliar. – sam Feb 02 '18 at 19:46
  • i added the main function to the end of the script to call the init() but i get this error : `can't load package: package .: ` `main.go:1:2: expected 'package', found 'EOF'` – sam Feb 02 '18 at 20:12
  • @sam_com: you don't call `init()`, it's called automatically during package initialization. – JimB Feb 02 '18 at 20:41
  • I see, then how should I implement the main function, so I can generate an executable binary file using go build ? also is `grumpy/build/src/grumpy` a correct path? – sam Feb 02 '18 at 20:50

1 Answers1

0

I know it is an old post but following code in python file may work:

def hello():
        print("hello, world")
if __name__ == "__main__": 
    hello()

Now go will create its own main function and is likely to work. (disclaimer: I have not yet tried this myself).

rnso
  • 23,686
  • 25
  • 112
  • 234