6

How can I pass an array of functions to my main function Validate? I cant get the right syntax for this

package main

import (
    "fmt"
)

func upper(input string) string {

    return "hola"
}

func Validate(spec string, validations []func(string) string) {

    for err, exec := range validations {
        fmt.Println(exec(spec))
    }
}



func main() {
    Validate("Hola", []func{upper})
}

Regards!

1 Answer 1

11

Here is correct example of using slice arguments. Before using slice literal you need to specify it's type.

package main

import "fmt"

func upper(input string) string {
    return "hola"
}

func Validate(spec string, validations []func(string) string) {
    for _, exec := range validations {
        fmt.Println(exec(spec))
    }
}

func main() {
    Validate("Hola", []func(string) string{upper})
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.