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

Creating a function receiver #113

Closed
darren-west opened this issue Nov 6, 2023 · 2 comments
Closed

Creating a function receiver #113

darren-west opened this issue Nov 6, 2023 · 2 comments

Comments

@darren-west
Copy link

darren-west commented Nov 6, 2023

Hi

Im trying to create a function that has a receiver of a struct type. For the life of me I cant work out how to do this?

Is it possible or not an implemented feature?

for example generate the code

type Foo struct{}

func (f Foo) Print(){
	fmt.Printf("%v\n", f)
}

Thanks

@dave
Copy link
Owner

dave commented Nov 6, 2023

Like this?

func ExampleStructReceiver() {
	f := NewFile("main")
	f.Type().Id("Foo").Struct()
	f.Func().Params(Id("f").Id("Foo")).Id("Print").Params().Block(
		Qual("fmt", "Printf").Call(Lit("%v\n"), Id("f")),
	)
	fmt.Printf("%#v", f)
	// Output:
	// package main
	//
	// import "fmt"
	//
	// type Foo struct{}
	//
	// func (f Foo) Print() {
	// 	fmt.Printf("%v\n", f)
	// }
}

@darren-west
Copy link
Author

Yea exactly that thanks, I acutally figured that out just after I asked and then lost internet connection and you beat me to closing it.

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

2 participants