Skip to content

What is canonical way to execute target function as a regular function? #495

Closed Answered by natefinch
mloskot asked this question in Q&A
Discussion options

You must be logged in to vote

Mage code is just go code. The examples sometimes ignore the receiver variable in mg.Namespace methods, but you can put it back. Then you can just call stack.Clean like you would in any other struct.

//go:build mage
// +build mage

package main

import (
	"fmt"

	"github.com/magefile/mage/mg"
)

type Stack mg.Namespace

//    ↓ add the receiver back
func (s Stack) Build(name string) error {
	fmt.Println("Building", name)
        
	s.Clean(name) // then you can use it like normal

	return nil
}

func (Stack) Clean(name string) error {
	fmt.Println("Cleaning", name)
	return nil
}

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@mloskot
Comment options

@natefinch
Comment options

Answer selected by mloskot
@mloskot
Comment options

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