Anonymous Functions in Golang

Anonymous functions in Go are very useful. Here is the syntax, usage and example of using anonymous functions in Golang .

Anonymous Functions in Golang

An anonymous function is a function that has no name. It is useful when you want to create an inline function. In Go, anonymous functions can also form a closure. Anonymous functions are also called function literals.

For example:

package main
import "fmt"

func main() {
    // Hàm ẩn danh
    func() {
        fmt.Println("Welcome! to Quantrimang.com")
    }()
}

Result:

Welcome! to Quantrimang.com

Syntax:

func(parameter_list)(return_type) {
    // code..

    // Dùng lệnh return nếu return_type được đưa ra
    // nếu return_type không được cung cấp, thì không
    // dùng lệnh return
    return
}()

Assign a variable

You can assign an anonymous function to a variable. This variable can then be called like a regular function.

package main
import "fmt"

func main() {
    // Gán một hàm ẩn danh cho một biến
    value := func() {
        fmt.Println("Welcome! to Quantrimang.com")
    }
    value()
}

Result:

Welcome! to Quantrimang.com

Pass as argument

You can also pass an anonymous function as an argument to another function.

package main
import "fmt"

// Chuyển hàm ẩn danh làm đối số
func GFG(i func(p, q string) string) {
    fmt.Println(i("Geeks", "for"))
}
func main() {
    value := func(p, q string) string {
        return p + q + "Geeks"
    }
    GFG(value)
}

Result:

GeeksforGeeks

Return anonymous function

You can return an anonymous function from another function.

package main
import "fmt"

// Trả về hàm ẩn danh
func GFG() func(i, j string) string {
    myf := func(i, j string) string {
        return i + j + "GeeksforGeeks"
    }
    return myf
}

func main() {
    value := GFG()
    fmt.Println(value("Welcome ", "to "))
}

Result:

Welcome to GeeksforGeeks

Conclude

Anonymous functions in Golang  are very flexible and powerful. They can be used to create inline functions, closures, and even to pass and return functions. Understanding how to use them effectively can greatly improve your programming skills in Go.

Leave a Comment

Best Halloween Team Building Games to Play at the Workplace

Best Halloween Team Building Games to Play at the Workplace

Explore seven Halloween team building games for the workplace, with quick rules, hybrid-friendly options, accessibility tips, and practical safety guidance.

Make Realistic Fake Blood Without a Cleanup Surprise

Make Realistic Fake Blood Without a Cleanup Surprise

Mix a glossy prop-only fake blood batch, choose safer products for skin and costumes, and test cleanup before Halloween night.

Kawaii Halloween Decor Without Gore or Jump Scares

Kawaii Halloween Decor Without Gore or Jump Scares

Create a gentle Halloween look with pastel colors, friendly ghosts, easy no-carve accents, and practical safety tips for your home.

Desk Decorating Ideas for a Spooky Office Cubicle

Desk Decorating Ideas for a Spooky Office Cubicle

Create a spooky office cubicle with Halloween desk decor ideas that keep your workspace usable, tidy, and mindful of workplace safety.

Pop Culture Costume Ideas That Will Dominate Halloween 2026

Pop Culture Costume Ideas That Will Dominate Halloween 2026

Plan a recognizable Halloween 2026 look with pop culture costumes inspired by Spider-Man, Supergirl, Toy Story 5, Moana, Mario, and Wednesday.

Leftover Halloween Candy and Pumpkins: What to Do Next

Leftover Halloween Candy and Pumpkins: What to Do Next

Sort leftover Halloween candy safely, share or repurpose sealed treats, and choose food use or compost for pumpkins based on their condition.

What Not to Wear: A Guide to Professional Workplace Halloween Etiquette

What Not to Wear: A Guide to Professional Workplace Halloween Etiquette

Compare workplace Halloween costume choices by respect, dress code, mobility, and safety—and learn when a subtle accessory or a policy check is the better option.

Couples Costume Ideas for 2026: Iconic Pairs and Clever Tropes

Couples Costume Ideas for 2026: Iconic Pairs and Clever Tropes

Find a couples costume that fits your style, budget, and comfort level, from Addams Family classics to original inside-joke duos.

Last-Minute High School Halloween Costumes That Take 5 Minutes

Last-Minute High School Halloween Costumes That Take 5 Minutes

Need a last-minute high school Halloween costume? Compare 10 easy closet-based ideas by prep time, comfort, school-day practicality, and simple safety checks.

When to Start Decorating for Halloween in 2026

When to Start Decorating for Halloween in 2026

Use a simple 2026 Halloween decorating timeline for indoor décor, outdoor displays, and fresh pumpkins, with practical adjustments for weather and events.