Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

What ́s the proper way to call a struct method from within an expressio ? #220

Unanswered
tkoberle asked this question in Q&A
Discussion options

I cannot seem to get the proper way to call a struct ́s method from within an application. I assume its due to fact that parameters are passed in via an interface{} field but I cant find any information on how to get around it.

I get a panic like this one :
panic: type main.Product has no method GetTicker (1:11)
| MyProduct.GetTicker()
| ..........^

Can you please advise ? Here ́s a sample code snippet

https://go.dev/play/p/_ziiXltEKue

Regards

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

type Product struct {
	Ticker string
}
func (p *Product) GetTicker() string {
	return p.Ticker
}
type Env struct {
+	MyProduct *Product
-	MyProduct Product
}
func main() {
	code := `MyProduct.GetTicker()`
	program, err := expr.Compile(code, expr.Env(Env{}))
	if err != nil {
		panic(err)
	}
	env := Env{
+		MyProduct: &Product{
-		MyProduct: Product{
			Ticker: "PETR4",
		},
	}
	output, err := expr.Run(program, env)
	if err != nil {
		panic(err)
	}
	fmt.Println(output)
}
You must be logged in to vote
1 reply
Comment options

Thanks Anton

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /