Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Methods definition #351

Open
c4s4 opened this issue Jan 12, 2022 · 0 comments
Open

Methods definition #351

c4s4 opened this issue Jan 12, 2022 · 0 comments

Comments

@c4s4
Copy link
Contributor

c4s4 commented Jan 12, 2022

In Go, a method defined on a pointer like this:

func (u *User) SayHello() {...}

May be called on a pointer or on an instance:

user1 := &User{Name: "Michel"}
user1.SayHello()
user2 := User{Name: "Michel"}
user2.SayHello()

But this is not the case in Anko, as following example demonstrates:

package main

import (
	"fmt"
	"log"

	"github.com/mattn/anko/env"
	"github.com/mattn/anko/vm"
)

type User struct {
	Name string
}

func (u *User) SayHello() {
	fmt.Printf("Hello %s!\n", u.Name)
}

func main() {
	e := env.NewEnv()
	user := User{Name: "Michel"}
	user.SayHello()
	err := e.Define("user", user)
	if err != nil {
		log.Fatalf("Define error: %v\n", err)
	}
	_, err = vm.Execute(e, nil, "user.SayHello()")
	if err != nil {
		log.Fatalf("Execute error: %v\n", err)
	}
}

Which prints:

$ go run main.go 
Hello Michel!
2022/01/12 18:42:13 Execute error: no member named 'SayHello' for struct
exit status 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant