main.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/tliron/py4go"
  5. "github.com/tliron/py4go/examples/hello-world/api"
  6. )
  7. func main() {
  8. python.PrependPythonPath(".")
  9. python.Initialize()
  10. defer python.Finalize()
  11. fmt.Printf("Go >> Python version:\n%s\n", python.Version())
  12. fmt.Println()
  13. fmt.Println("Go >> Type checking:")
  14. float, _ := python.NewPrimitiveReference(1.0)
  15. fmt.Printf("Go >> IsFloat: %t\n", float.IsFloat())
  16. fmt.Println()
  17. api, _ := api.CreateModule()
  18. api.EnableModule()
  19. defer api.Release()
  20. foo, _ := python.Import("foo")
  21. defer foo.Release()
  22. fmt.Println("Go >> Calling a Python function:")
  23. hello, _ := foo.GetAttr("hello")
  24. defer hello.Release()
  25. r, _ := hello.Call("Tal")
  26. defer r.Release()
  27. r_, _ := r.ToString()
  28. fmt.Printf("Go >> Python function returned: %s\n", r_)
  29. fmt.Println()
  30. fmt.Println("Go >> Calling a Python method:")
  31. person, _ := foo.GetAttr("person")
  32. defer person.Release()
  33. greet, _ := person.GetAttr("greet")
  34. defer greet.Release()
  35. greet.Call()
  36. fmt.Println()
  37. fmt.Println("Go >> Python exception as Go error:")
  38. bad, _ := foo.GetAttr("bad")
  39. defer bad.Release()
  40. if _, err := bad.Call(); err != nil {
  41. fmt.Printf("Go >> Error message: %s\n", err)
  42. }
  43. fmt.Println()
  44. goodbye, _ := foo.GetAttr("goodbye")
  45. defer goodbye.Release()
  46. goodbye.Call()
  47. sayName, _ := foo.GetAttr("say_name")
  48. defer sayName.Release()
  49. sayName.Call()
  50. sayNameFast, _ := foo.GetAttr("say_name_fast")
  51. defer sayNameFast.Release()
  52. sayNameFast.Call()
  53. }