선택적 파라미터를 언급하고자 한다. 한가지 예외적인 경우는 다음 장에서 살펴보겠지만, 함수
를 위한 파라미터는 호출 시 모두 넘겨져야 한다. 이름이 지정된 파라미터나 선택적 파라미터
처럼 사용하고 싶다면, 파라미터로 사용될 것과 동일하게 구조체로 만들어 함수로 넘겨줘야 한
다. [예제
5
-
1
]로 이런 경우를 실습해보도록 하자.
예제
5-1
명명된 파라미터를 위해 구조체 사용
type MyFuncOpts struct {
FirstName string
LastName string
Age int
}
func MyFunc(opts MyFuncOpts) error {
// do something here
}
func main() {
MyFunc(MyFuncOpts {
LastName: “Patel”,
Age: 50,
})
My Func(MyFuncOpts {
FirstName: “Joe”,
LastName: “Smith”,
})
}
실제로 이름이 지정된 파라미터와 선택적 파라미터를 갖지 않더라도 제한은 아니다. 함수는 파
라미터가 몇 개 이상을 가져서는 안 되며, 이름이 지정된 파라미터와 선택적 파라미터는 함수 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.