go에있는 빌트인 함수인 append를 공식문서에서 찾아봤습니다.
https://go.dev/doc/effective_go#append

 

Effective Go - The Go Programming Language

Documentation Effective Go Effective Go Introduction Go is a new language. Although it borrows ideas from existing languages, it has unusual properties that make effective Go programs different in character from programs written in its relatives. A straigh

go.dev

 

 

Now we have the missing piece we needed to explain the design of the append built-in function. The signature of append is different from our custom Append function above. Schematically, it's like this:

이제 우리는 append 내장 함수의 디자인을 설명하기 위해서 우리가 필요했던 잃어버린 조각(위에서 부족한 설명에 대한 답)을 가지고 있다. append의 특징은 위에서 본 우리의 커스텀 Append 함수(https://go.dev/doc/effective_go#slices)와 다르다. 개략적으로 이것은 다음과 같다:

 

func append(slice []T, elements ...T) []T

where T is a placeholder for any given type. You can't actually write a function in Go where the type T is determined by the caller. That's why append is built in: it needs support from the compiler.

여기에서 T는 어떤 주어진 타입에 대한 placeholder(제네릭, 일시적으로 값을 대체하거나 나중에 채워질 것으로 예상되는 위치 )이다. Go에서는 타입 T가 호출자에 의해 결정되는 함수를 작성할 수 없다. 이것이 append가 내장함수인 이유이고: 컴파일러의 지원이 필요하다.

 

위의 문장에서 보충설명 : 제네릭 T를 사용할 때 타입을 명시해줘야 된다. 안그러면 오류가 난다. 하지만 위에 append 함수는 호출하는 쪽에서 타입을 정할수 있다. 컴파일러의 지원덕분에 가능. 내장함수 append는 a := append([]int{}, 1, 2, 3)처럼 호출하는 쪽에서 []int로 정할수 있다. 하지만 커스텀 함수에서 제네릭은 이렇게 타입을 명시해줘야함. 여기서는 any로 해줌.

 

func Print[T any](a T) {
   fmt.Println(a)
}

What append does is append the elements to the end of the slice and return the result. The result needs to be returned because, as with our hand-written Append, the underlying array may change. This simple example

append가 하는 것은 요소들을 slice끝에 삽입하고, 결과를 리턴하는 것이다. 우리의 직접 작성된 Append(https://go.dev/doc/effective_go#slices) 와 같이 실제 배열(슬라이스 안에 배열을 가리키는 포인터)은 바뀔수도 있기 때문에, 결과는 반환되어야할 필요가 있다. 이 간단한 예제는

 

x := []int{1,2,3}
x = append(x, 4, 5, 6)
fmt.Println(x)

prints [1 2 3 4 5 6]. So append works a little like Printf, collecting an arbitrary number of arguments.

[1 2 3 4 5 6]을 출력한다. 그래서 append는 약간 Printf처럼 동작하고, 임의의 인자들의 수를 수집한다.

 

But what if we wanted to do what our Append does and append a slice to a slice? Easy: use ... at the call site, just as we did in the call to Output above. This snippet produces identical output to the one above.

그러나 우리의 Append가 하는 것을 하길 원하고, 슬라이스에 슬라이스를 추가하고 싶다면 어떻게 할까? 쉽다: call site(함수 인자에)...을 사용해라, 위에서(https://go.dev/doc/effective_go#Printing) Output(std.Output 함수) 호출에서 했던 것처럼. 아래 snippet(작은 예제)은 위 예제와 동일한 출력을 만든다.

 

위의 문장에서 보충설명 : 위의 custom 함수 Append와 같은 기능을 하고 슬라이스에 슬라이스를 추가하고 싶을 때를 설명하고 있다. 위에 func Println(v ...interface{}) { std.Output(2, fmt.Sprintln(v...))  // Output takes parameters (int, string) } 함수에서 했던 것처럼 ...을 사용 하라는 의미인것으로 보인다.

 

x := []int{1,2,3}
y := []int{4,5,6}
x = append(x, y...)
fmt.Println(x)

Without that ..., it wouldn't compile because the types would be wrong; y is not of type int.

...없이, 위의 예제는 타입들이 틀리기 때문에 컴파일되지 않을 것이다; 예시 : y는 int 타입이 아니다.

+ Recent posts