목차

Go 사용자 정의 타입 - 함수 타입

함수 시그니처를 타입으로 정의하여 변수처럼 사용할 수 있다.

package main

import "fmt"

type totCost func(int, int) int

func describe(cnt int, price int, fn totCost) {
    fmt.Printf("cnt: %d, price: %d, orderPrice: %d", cnt, price, fn(cnt, price))
}

func main() {
    var orderPrice totCost
    orderPrice = func(cnt int, price int) int {
        return (cnt * price) + 1000000
    }
    describe(3, 10000000, orderPrice)
}