作为golang开发,golang 中 make 和 new 的区别?


在Go语言中,`make`和`new`是用于创建不同类型的对象的内建函数。它们在用途和返回值方面有一些区别。 1. `new`函数: `new`函数用于分配内存空间,并返回指向该内存空间的指针。它接受一个类型作为参数,并返回一个指向该类型的新分配的零值的指针。这意味着,使用`new`创建的对象会被初始化为零值。 例如,使用`new(T)`会分配一个新的`T`类型的零值对象,并返回指向该对象的指针。 ```go t := new(T) ``` 2. `make`函数: 相比之下,`make`函数用于创建并初始化引用类型(如切片、映射和通道)的对象。`make`函数接受一个类型、长度和容量(可选)作为参数,并返回初始化后的对象。与`new`不同,`make`不仅分配内存空间,还会进行初始化操作。 例如,使用`make([]T, length, capacity)`会创建一个初始长度为`length`、初始容量为`capacity`的新的切片对象,并返回该切片对象。 ```go s := make([]T, length, capacity) ``` `new`函数用于分配内存空间并返回指向新分配的零值对象的指针,适用于所有类型。 而`make`函数用于创建并初始化引用类型的对象,如切片、映射和通道等。它接受类型、长度和容量作为参数,并返回初始化后的对象。 这两者的主要区别在于它们的应用范围和返回值。 [2023-11-09 18:10:41 | AI写代码神器 | 346点数解答]
相关提问
- + make tini-static Scanning dependencies of target tini-static [100%] Building C object CMakeFiles/tini-static.dir/src/tini.c.o Linking C executable tini-static /usr/bin/ld: cannot find -lc collect2: error: ld returned 1 exit status make[3]: *** [tini-static] Error 1 make[2]: *** [CMakeFiles/tini-static.dir/all] Error 2 make[1]: *** [CMakeFiles/tini-static.dir/rule] Error 2 make: *** [tini-static] Error 2 error: Bad exit status from /var/tmp/rpm-tmp.0XoVnu (%build)(264点数解答 | 2025-05-24 02:02:15)171
- + make tini-static Scanning dependencies of target tini-static [100%] Building C object CMakeFiles/tini-static.dir/src/tini.c.o Linking C executable tini-static /usr/bin/ld: cannot find -lc collect2: error: ld returned 1 exit status make[3]: *** [tini-static] Error 1 make[2]: *** [CMakeFiles/tini-static.dir/all] Error 2 make[1]: *** [CMakeFiles/tini-static.dir/rule] Error 2 make: *** [tini-static] Error 2 error: Bad exit status from /var/tmp/rpm-tmp.0XoVnu (%build)(571点数解答 | 2025-05-24 02:03:08)134
- 详细解释下面的代码: // netty基本操作,两个线程组 bossGroup = new NioEventLoopGroup(); workerGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2); try { // netty的启动类 ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) //记录日志的handler,netty自带的 .childHandler(new HelloServerInitializer()) .option(ChannelOption.SO_BACKLOG, 2048). childOption(ChannelOption.SO_KEE(244点数解答 | 2025-01-17 18:26:07)145
- 作为golang开发,golang 中 make 和 new 的区别?(346点数解答 | 2023-11-09 18:10:41)322
- 作为c++开发,new int[10]跟new int[10]()区别(196点数解答 | 2023-11-09 00:45:31)234
- void createlist(listnode* &phead) { phead - new listnode(0): listnodc*p= phcad; for (inti-1;i<10;i++){ listnode*ptmp= new listnode(i); p->next-ptmp; p-ptmp; 创建循环链表 void createlooplist(listnode*&phead) phead-new listnode(0); listnode*p=phead: int length-10; listnode* loopstart-null: for (int i=1: i <length:i++) { listnode *ptmp-new listnode(i): p->next-ptmp; p-ptmp. it (i6i loopstart=ptmp; p->next-loopstart; int lengthofcop(listnode*phead /todo:实现此函数,返回环形链表的环的长度,如果静表无环,返回0 return 0; intmaino t l(193点数解答 | 2024-10-09 16:49:52)167
- void createlist(listnode* &phead) { phead - new listnode(0): listnodc*p= phcad; for (inti-1;i<10;i++){ listnode*ptmp= new listnode(i); p->next-ptmp; p-ptmp; 创建循环链表 void createlooplist(listnode*&phead) phead-new listnode(0); listnode*p=phead: int length-10; listnode* loopstart-null: for (int i=1: i <length:i++) { listnode *ptmp-new listnode(i): p->next-ptmp; p-ptmp. it (i6i loopstart=ptmp; p->next-loopstart; int lengthofcop(listnode*phead /todo:实现此函数,返回环形链表的环的长度,如果静表无环,返回0 return 0; 确定环的起始位置 int(519点数解答 | 2024-10-09 17:31:13)189
- void createlist(listnode* &phead) { phead - new listnode(0): listnodc*p= phcad; for (inti-1;i<10;i++){ listnode*ptmp= new listnode(i); p->next-ptmp; p-ptmp; 创建循环链表 void createlooplist(listnode*&phead) phead-new listnode(0); listnode*p=phead: int length-10; listnode* loopstart-null: for (int i=1: i <length:i++) { listnode *ptmp-new listnode(i): p->next-ptmp; p-ptmp. it (i6i loopstart=ptmp; p->next-loopstart; 实现此函数,返回环形链表的起始位置(438点数解答 | 2024-10-09 17:37:28)195
- using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace teachingmanagementsystem { class program { static void main(string[] args) { console.writeline("欢迎使用简易教学管理系统"); // 创建课程列表 list<course> courses = new list<course> { new course("数学"), new course("英语"), new course("物理") }; // 创建学生(419点数解答 | 2024-10-24 17:49:32)168
- public LineInfo selLineInfo(LineInfo lineInfo) throws Exception { LineBasicInfo lineBasicInfo = new LineBasicInfo(); Date nowTime = new Date(); SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); LineInfo lineInfo2 = new LineInfo(); if (lineInfo.getId() == 0) { lineBasicInfo.setLineName("M1"); lineInfo2.setLineName("**T2_M1"); } else if (lineInfo.getId() == 1) { lineBasicInfo.setLineName((1080点数解答 | 2025-03-04 15:24:59)117
- public LineInfo selLineInfo(LineInfo lineInfo) throws Exception { LineBasicInfo lineBasicInfo = new LineBasicInfo(); Date nowTime = new Date(); SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); LineInfo lineInfo2 = new LineInfo(); if (lineInfo.getId() == 0) { lineBasicInfo.setLineName("M1"); lineInfo2.setLineName("**T2_M1"); } else if (lineInfo.getId() == 1) { lineBasicInfo.setLineName((1080点数解答 | 2025-03-04 15:27:11)136
- 帮我用python完成这个算法分析与设计的课程实验: question 1. implement the kruskal’s algorithm in mst as sortededges() method in a disjoint-sets data structure. the disjoint-sets can be implimented by lists or trees. the underlying data structure of the disjoint-sets maintains a collection of disjoint sets such that each set has a unique representative element and supports the following operations: 1. makeset(u): make a new set containing element u. 2. union(u, v): merge the sets containing u and v. 3. find(u): retur(1636点数解答 | 2023-12-29 16:57:35)317