Skip to content

Question - How can I pass a struct to a scripted function #1271

Answered by ashneverdawn
p4tin asked this question in Q&A
Discussion options

You must be logged in to vote

Don't know if there's a way to do it without reflection, but here's what I did which worked for me:

package main

//Note the additional imports
import (
	"github.com/containous/yaegi/interp"
	"github.com/containous/yaegi/stdlib"
	"reflect"
)

//Note 'custom' lib was imported
const src = `package foo
import "custom"
func Bar(d *custom.Data) string { return d.Message + "-Foo" }`

type Data struct {
	Message string
}

func main() {
	d := Data{
		Message: "Kung",
	}
	
	i := interp.New(interp.Options{})
	
	//This allows use of standard libs
	i.Use(stdlib.Symbols)
	
	//This will make a 'custom' lib  available that can be imported and contains your Data struct
	custom := make(map[string]map[stri…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@skydiver89
Comment options

@RedUndercover
Comment options

@Gandalf-Le-Dev
Comment options

@skydiver89
Comment options

Answer selected by mvertes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants
Converted from issue

This discussion was converted from issue #478 on September 30, 2021 09:23.