Golang new/make
Golang New/Make
golang์์ ์๋ก์ด ๋ณ์๋ฅผ ํ ๋นํ ๋ ์ฃผ๋ก new/make ๋ ๊ฐ์ง๋ฅผ ์ฌ์ฉํ๋ค.
๋นํธ์ธ ํจํค์ง์์๋ ๋ ํจ์๋ ์ฌ์ด์ข๊ฒ ๋ถ์ด์๋ค.
์ด๋ค ์ฐจ์ด์ ์ด ์์๊น?
package builtin
// The make built-in function allocates and initializes an object of type
// slice, map, or chan (only). Like new, the first argument is a type, not a
// value. Unlike new, make's return type is the same as the type of its
// argument, not a pointer to it. The specification of the result depends on
// the type:
//
// Slice: The size specifies the length. The capacity of the slice is
// equal to its length. A second integer argument may be provided to
// specify a different capacity; it must be no smaller than the
// length. For example, make([]int, 0, 10) allocates an underlying array
// of size 10 and returns a slice of length 0 and capacity 10 that is
// backed by this underlying array.
// Map: An empty map is allocated with enough space to hold the
// specified number of elements. The size may be omitted, in which case
// a small starting size is allocated.
// Channel: The channel's buffer is initialized with the specified
// buffer capacity. If zero, or the size is omitted, the channel is
// unbuffered.
func make(t Type, size ...IntegerType) Type
// The new built-in function allocates memory. The first argument is a type,
// not a value, and the value returned is a pointer to a newly
// allocated zero value of that type.
func new(Type) *Type์ฌ์ค ๋ค์ด์ค๋ ํ์ ๊ณผ, ๋ฆฌํด ํ์ ๋ง ๋ด๋ ์ฐจ์ด๋ฅผ ์ ์ ์๋ค.
์ผ๋จ ํ ๋ฒ ์์๋ฅผ ๋ณด์. new์ ์ฌ์ฉ์ ๋ค์๊ณผ ๊ฐ๋ค.
๊ฒฐ๊ณผ๊ฐ์ ์ฃผ์๊ฐ ๋์จ๋ค. new๋ ์ฃผ์๊ฐ์ ๋ฆฌํดํ๋ค๋ ๊ฒ์ ์ ์ ์๋ค.
make๋ ์ด๋จ๊น?
make๋ ๊ฐ ์์ฒด๋ฅผ ๋ฐํํ๋ค.
์ฆ, make์ new์ ์ฃผ์ ์ฐจ์ด์ ์ ํฌ์ธํฐ์ด๋ ์๋๋์ด๋ค.
new๋ 0์ผ๋ก ์ด๊ธฐํ๋ ๊ฐ์ ์์ฑํ๊ณ ํด๋น ๊ฐ์ ์ฃผ์๋ฅผ ๋ฐํํ๋ ๋ฐ๋ฉด, make๋ ์ด๊ธฐํ๋ ๊ฐ์ ์์ฑํ๊ณ ํด๋น ๊ฐ์ ์ง์ ๋ฐํํ๋ค.
๋ํ, make๋ ์ฌ๋ผ์ด์ค, ๋งต, ์ฑ๋ ๊ฐ์ ๋ด์ฅ ํ์ ์๋ง ์ฌ์ฉ๋ ์ ์๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก ๋ง์ด๋ค ์ฌ์ฉํ๋ ๋ ๋นํธ์ธ ํจ์์ ์ฐจ์ด์ ์ ๋ํด์ ์ ์๊ณ ์์ผ๋ฉด ์ข์ ๊ฒ์ด๋ค.
Last updated