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