[Golang] Anonymous func

golang anonymous func

  • Anonymous func 是沒有 identifier 的 func
    • 一樣可以定義、傳入參數
    • 程式碼:
      package main
      
      import (
      	"fmt"
      )
      
      func main() {
      	// anonymous func
      	foo()
      
      	func() {
      		fmt.Println("Anonymous func ran")
      	}()
      
      	func(x int) {
      		fmt.Println("The meaning of life:", x)
      	}(42)
      }
      
      func foo() {
      	fmt.Println("foo ran")
      }
      

       

    • End