> For the complete documentation index, see [llms.txt](https://rafli-ramadhan.gitbook.io/golang-example-code-by-topic/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rafli-ramadhan.gitbook.io/golang-example-code-by-topic/tipe-data-reference/function-in-golang/function-sebagai-parameter.md).

# Function sebagai Parameter

Function bisa digunakan sebagai parameter untuk function yang lain.

## Contoh *code* 1

```go
package main

import (
	"fmt"
)

// type declaration
type Filter func(name string) string

// function as parameter
func sayHelloWithFilter(name string, filter Filter) {
	nameFiltered := filter(name)
	fmt.Println("Hello", nameFiltered)
}

func spamFilter(name string) string {
	if name == "Kucing" {
		return ""
	} else {
		return name
	}
}
	
func main() {
	filter := spamFilter
	sayHelloWithFilter("Alan", filter)
	sayHelloWithFilter("Hana", filter)
}
```

```
Hello Alan
Hello Hana
```

## Contoh *code* 2

```go
package main

import (
	"fmt"
	"strings"
)

// function as parameter
func IsDirty(word string, filterWord func(string) bool) {
	if filterWord(word) {
		fmt.Println("That is dirty word")
	} else {
		fmt.Println("That is not dirty word")
	}
}
	
func main() {
	// anonymous function
	filterWord := func(word string) bool {
		if strings.Contains(word, "fuck") {
			return true
		} else {
			return false
		}
	}

	IsDirty("hi", filterWord)
	IsDirty("fuck", filterWord)
}

```

```
That is not dirty word
That is dirty word
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rafli-ramadhan.gitbook.io/golang-example-code-by-topic/tipe-data-reference/function-in-golang/function-sebagai-parameter.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
