MENU

Go语言new和make的区别

April 25, 2022 • Golang

要知道new和make的区别,我们可以先看看他们俩的函数描述

// 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
// 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

从上面的描述我们大致可以看到:

  1. new的主要作用是分配内存,通过传入一个类型(type),然后返回一个对应的类型的指针,默认是零值
  2. make的主要作用也是分配内存,不过与new不同的是,make只能处理slicemap 以及channel这三种类型的数据类型,并且返回的数据类型也并非指针,而是直接返回对应的数据类型

接下来我们通过几个代码片段来了解一下:

/*
 * @Author: NorthCity1984
 * @LastEditTime: 2022-04-24 21:50:24
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCity1984 All rights reserved.
 */
package main

import "fmt"

func main() {
  // new()
  var a *int
  *a = 12  // runtime error: invalid memory address or nil pointer dereference
  fmt.Println(*a) 
}

上面这个代码片段,是很多go选手都可能会范的错误,我们声明了一个指针类型的数据,但是没有给这个数据分配具体的空间就直接给这个数据去赋值,这样是会报错的,为了解决这个问题,我们可以用new关键字先分配一块空间

/*
 * @Author: NorthCity1984
 * @LastEditTime: 2022-04-25 13:53:05
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCity1984 All rights reserved.
 */
package main

import "fmt"

func main() {
  // new()
  var a *int = new(int)
  *a = 12
  fmt.Println(*a) // 12
}

嗯,如果你学过C++的话,这里应该比较好理解

我们再看看make

/*
 * @Author: NorthCity1984
 * @LastEditTime: 2022-04-25 14:03:26
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCity1984 All rights reserved.
 */
package main

import "fmt"

type person struct {
  Age  int
  Name string
}

func main() {
  var ch1 = make(chan int, 10)
  var slice1 = make([]int, 10, 15)
  var map1 = make(map[string]string)

  ch1 <- 12
  fmt.Println(<-ch1)

  fmt.Println(slice1, len(slice1), cap(slice1))

  map1["hello"] = "world"
  map1["hello1"] = "world1"
  map1["hello2"] = "world2"

  for key, value := range map1 {
    fmt.Println(key, value)
  }
}
作者:NorthCity1984
出处:https://grimoire.cn/golang/new-make.html
版权:本文《Go语言new和make的区别》版权归作者所有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任