search
HomeBackend DevelopmentGolangWhat is the difference between make and new in go language

What is the difference between make and new in go language

Jan 09, 2023 am 11:44 AM
golanggo languagemakenew

Difference: 1. Make can only be used to allocate and initialize data of types slice, map, and chan; while new can allocate any type of data. 2. New allocation returns a pointer, which is the type "*Type"; while make returns a reference, which is Type. 3. The space allocated by new will be cleared; after make allocates the space, it will be initialized.

What is the difference between make and new in go language

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

new and make are primitives for memory allocation in the Go language. Simply put, new only allocates memory, and make is used to initialize slices, maps, and channels.

new

new(T) function is a built-in function that allocates memory. It allocates a piece of memory for each type and initializes it to zero. value and returns its memory address.

The syntax is func new(Type) *Type

As we all know, an existing variable can be assigned to its pointer.

var p int
var v *int
v = &p
*v = 11
fmt.Println(*v)

So what if it’s not a variable yet? Can you assign it directly?

func main() {
	var v *int
	*v = 8
	fmt.Println(*v)

	// panic: runtime error: invalid memory address or nil pointer dereference
	// [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x47df36]

	// goroutine 1 [running]:
	// main.main()
	// 	/tmp/sandbox1410772957/prog.go:9 +0x16
}

The error result is as shown in the comments in the code.

How to solve? This can be solved by Go providing new initialization address.

func main() {
	var v *int
	// v 是一个 int 类型的指针,v 的地址和 v 的值  0xc0000ba018 <nil>
	fmt.Println("v 是一个 int 类型的指针,v 的地址和 v 的值 ", &v, v)   
	// 分配给 v 一个指向的变量             
	v = new(int)    
	// v 是一个 int 类型的指针,v 的地址和 v 的值  0xc0000ba018 0xc000018030 0,此时已经分配给了 v 指针一个指向的变量,但是变量为零值                                                  
	fmt.Println("v 是一个 int 类型的指针,v 的地址, v 的值和 v 指向的变量的值 ", &v, v, *v) 
	*v = 8
	// v 是一个 int 类型的指针,v 的地址和 v 的值  0xc0000ba018 0xc000018030 8,此时又像这个变量中装填了一个值 8
	fmt.Println("v 是一个 int 类型的指针,v 的地址, v 的值和 v 指向的变量的值 ", &v, v, *v) 
	
	// 整个过程可以理解为给 v 指针指向了一个匿名变量
}

What is the difference between make and new in go language

We can see that initializing a pointer variable with a value of nil is not a direct assignment. Return a pointer with a value of 0xc000018030 through new. points to the newly assigned int type, and zero value is its value.

Additionally, it is important to note that the zero value is different for different pointer types. For details, you can refer to this article. Or you can browse the code below.

type Name struct {
    P string
}
var av *[5]int
var iv *int
var sv *string
var tv *Name

av = new([5]int)
fmt.Println(*av) //[0 0 0 0 0 0]
iv = new(int)
fmt.Println(*iv) // 0
sv = new(string) 
fmt.Println(*sv) //
tv = new(Name)
fmt.Println(*tv) //{}

The above describes how to assign values ​​after processing ordinary types new(). Here is how to assign values ​​after processing composite types (array, struct). But here, I think the author of the original article is wrong, because for slice, map and channel, new can only open

array instance

func main() {
	// 声明一个数组指针
	var a *[5]int
	fmt.Printf("a: %p %#v \n", &a, a) //a: 0xc04200a180 [5]int{0, 0, 0, 0, 0}
	// 分配一个内存地址给 a(数组指针)指向
	a = new([5]int)
	fmt.Printf("a: %p %#v \n", &a, a) //av: 0xc000074018 &[5]int{0, 0, 0, 0, 0}
	// 修改这个数组中的值
	(*a)[1] = 8
	fmt.Printf("a: %p %#v \n", &a, a) //av: 0xc000006028 &[5]int{0, 8, 0, 0, 0}
}

structure instance

type mystruct struct {
	name string
	age  int
}

func main() {
	var people *mystruct
	people = new(mystruct)
	people.name = "zhangsan"
	people.age = 11

	fmt.Printf("%v, %v", people.name, people.age) // zhangsan, 11
}

make

make is specially used to create three types of content allocation: chan, map and slice, and can initialize them. The return type of make is the same type as its argument, not a pointer to it, because these three data types are themselves reference types.

The syntax is: func make(t Type, size ...IntegerType) Type, you can see that the second parameter is a variable length parameter, used to specify the size of the allocated memory, such as For slice, cap and length need to be specified (cap represents capacity, length represents length, that is, the size that can be used), and cap is required to be larger than length.

I won’t introduce too much about the cap and length of slice here. You can understand that there is a house now. This house is a rough house. All its rooms have 3 rooms (cap), and they have been decorated. 1 room (length).

As for why not use new to allocate memory for these three types? Let's do an experiment.

func main() {
	var s *[]int
	fmt.Printf("s 的地址是: %p, s 的值是 %p\n", &s, s) // s 的地址是: 0xc00000e028, s 的值是 0x0
	s = new([]int)
	fmt.Printf("s 的地址是: %p, s 的值是 %p\n", &s, s) // s 的地址是: 0xc00000e028, s 的值是 0xc00011a018
	(*s)[0] = 1
	fmt.Println("s 的地址是: %p, s 的值是 %p\n", &s, s) // panic: runtime error: index out of range [0] with length 0
}
}

You can see that the length is 0 when assigning a value to the slice. As for the specific reason, friends who know it can leave a message in the comment area.

Therefore, it is often recommended to use make to perform these three types of creation.

slice instance

func main() {
	// 第一个 size 是 length,第二个 size 是 cap
	a := make([]int, 5, 10)
	// a: 0xc00011a018 []int{0, 0, 0, 0, 0},cap: 10, length: 5 
	fmt.Printf("a: %p %#v,cap: %d, length: %d \n", &a, a, cap(a), len(a)) 
}

map instance

func main() {
	// 第一个 string 是 key,第二个 string 是 value
	mapInstance := make(map[string]string, 5)
	mapInstance["第一名"] = "张三"
	mapInstance["第二名"] = "李四"
	mapInstance["第三名"] = "王五"

	fmt.Println(mapInstance) // map[第一名:张三 第三名:王五 第二名:李四]
}

channel instance

func countNum(temp int, ch chan int) {
	i := temp + 1
	ch <- i
	fmt.Println("已经将 i 发往通道 c 中")
}

func main() {
	ch := make(chan int)
	go countNum(1, ch)
	res := <-ch
	fmt.Println("已经从 ch 中获取 i 并保存在 res 中")
	fmt.Println("res 是", res)
}

Summary:

The make function is only used for map, slice and channel, and does not return a pointer. If you want to get an explicit pointer, you can use the new function to allocate, or explicitly use the address of a variable.

The main differences between new and make in Go language are as follows:

  • make can only be used to allocate and initialize data of type slice, map, and chan; new can allocate Any type of data.

  • new allocation returns a pointer, which is the type *Type; make returns a reference, which is Type.

  • The space allocated by new is cleared; after make allocates the space, it will be initialized.

[Related recommendations: Go video tutorial, Programming teaching

The above is the detailed content of What is the difference between make and new in go language. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Go Binary Encoding/Decoding: A Practical Guide with ExamplesGo Binary Encoding/Decoding: A Practical Guide with ExamplesMay 07, 2025 pm 05:37 PM

Go's encoding/binary package is a tool for processing binary data. 1) It supports small-endian and large-endian endian byte order and can be used in network protocols and file formats. 2) The encoding and decoding of complex structures can be handled through Read and Write functions. 3) Pay attention to the consistency of byte order and data type when using it, especially when data is transmitted between different systems. This package is suitable for efficient processing of binary data, but requires careful management of byte slices and lengths.

Go 'bytes' Package: Compare, Join, Split & MoreGo 'bytes' Package: Compare, Join, Split & MoreMay 07, 2025 pm 05:29 PM

The"bytes"packageinGoisessentialbecauseitoffersefficientoperationsonbyteslices,crucialforbinarydatahandling,textprocessing,andnetworkcommunications.Byteslicesaremutable,allowingforperformance-enhancingin-placemodifications,makingthispackage

Go Strings Package: Essential Functions You Need to KnowGo Strings Package: Essential Functions You Need to KnowMay 07, 2025 pm 04:57 PM

Go'sstringspackageincludesessentialfunctionslikeContains,TrimSpace,Split,andReplaceAll.1)Containsefficientlychecksforsubstrings.2)TrimSpaceremoveswhitespacetoensuredataintegrity.3)SplitparsesstructuredtextlikeCSV.4)ReplaceAlltransformstextaccordingto

Mastering String Manipulation with Go's 'strings' Package: a practical guideMastering String Manipulation with Go's 'strings' Package: a practical guideMay 07, 2025 pm 03:57 PM

ThestringspackageinGoiscrucialforefficientstringmanipulationduetoitsoptimizedfunctionsandUnicodesupport.1)ItsimplifiesoperationswithfunctionslikeContains,Join,Split,andReplaceAll.2)IthandlesUTF-8encoding,ensuringcorrectmanipulationofUnicodecharacters

Mastering Go Binary Data: A Deep Dive into the 'encoding/binary' PackageMastering Go Binary Data: A Deep Dive into the 'encoding/binary' PackageMay 07, 2025 pm 03:49 PM

The"encoding/binary"packageinGoiscrucialforefficientbinarydatamanipulation,offeringperformancebenefitsinnetworkprogramming,fileI/O,andsystemoperations.Itsupportsendiannessflexibility,handlesvariousdatatypes,andisessentialforcustomprotocolsa

Implementing Mutexes and Locks in Go for Thread SafetyImplementing Mutexes and Locks in Go for Thread SafetyMay 05, 2025 am 12:18 AM

In Go, using mutexes and locks is the key to ensuring thread safety. 1) Use sync.Mutex for mutually exclusive access, 2) Use sync.RWMutex for read and write operations, 3) Use atomic operations for performance optimization. Mastering these tools and their usage skills is essential to writing efficient and reliable concurrent programs.

Benchmarking and Profiling Concurrent Go CodeBenchmarking and Profiling Concurrent Go CodeMay 05, 2025 am 12:18 AM

How to optimize the performance of concurrent Go code? Use Go's built-in tools such as getest, gobench, and pprof for benchmarking and performance analysis. 1) Use the testing package to write benchmarks to evaluate the execution speed of concurrent functions. 2) Use the pprof tool to perform performance analysis and identify bottlenecks in the program. 3) Adjust the garbage collection settings to reduce its impact on performance. 4) Optimize channel operation and limit the number of goroutines to improve efficiency. Through continuous benchmarking and performance analysis, the performance of concurrent Go code can be effectively improved.

Error Handling in Concurrent Go Programs: Avoiding Common PitfallsError Handling in Concurrent Go Programs: Avoiding Common PitfallsMay 05, 2025 am 12:17 AM

The common pitfalls of error handling in concurrent Go programs include: 1. Ensure error propagation, 2. Processing timeout, 3. Aggregation errors, 4. Use context management, 5. Error wrapping, 6. Logging, 7. Testing. These strategies help to effectively handle errors in concurrent environments.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment