golang的new函数_new()和make()函数以及Golang中的示例

golang的new函数

In Golang, to allocate memory, we have two built-in functions new() and make().

在Golang中,要分配内存,我们有两个内置函数new()和make()

1)new()函数 (1) new() function)

  • Memory returned by new() is zeroed.

    new()返回的内存为零。

  • new() only returns pointers to initialized memory.

    new()仅返回指向初始化内存的指针。

  • new() works for all the data types (except channel, map), and dynamically allocates space for a variable of that type and initialized it to zero value of that type and return a pointer to it.

    new()适用于所有数据类型(通道,映射除外),并为该类型的变量动态分配空间,并将其初始化为该类型的零值并返回指向它的指针。

Example:

例:

    result = new(int)

is equivalent to

相当于

    var temp int   // declare an int type variable
var result *int //  declare a pointer to int
result = &temp 

Example/program:

示例/程序:

There are three different ways to create a pointer that points to a zeroed structure value, each of which is equivalent:

有三种不同的方法可以创建指向零结构值的指针,每种方法都等效:

package main
import "fmt"
type Sum struct {
x_val int
y_val int
}
func main() {
// Allocate enough memory to store a Sum structure value
// and return a pointer to the value's address
var sum Sum
p := &sum
fmt.Println(p)
// Use a composite literal to perform 
//allocation and return a pointer
// to the value's address
p = &Sum{}
fmt.Println(p)
// Use the new function to perform allocation, 
//which will return a pointer to the value's address.
p = new(Sum)
fmt.Println(p)
}

Output

输出量

&{0 0}
&{0 0}
&{0 0}

2)make()函数 (2) make() function)

make() only makes slices, maps, and channels. make returns value of type T(data type) not *T

Example of slices:

make([]int, 10, 20) – Here, make creates the slice, and initialize its content depending on the default data type value. here int is used, so the default value is 0.

new([20]int)[0:10] – Here, It will also create slice but returns pointers to initialized memory.

Example/program:

There are two different ways to initialize a map which maps string keys to bool values are given below.

package main
import "fmt"
func main() {
// Using make() to initialize a map.
m := make(map[string]bool, 0)
fmt.Println(m)
// Using a composite literal to initialize a map.
m = map[string]bool{}
fmt.Println(m)
}

Output

Reference: allocation_new



TOP Interview Coding Problems/Challenges

  • Run-length encoding (find/print frequency of letters in a string)

  • Sort an array of 0's, 1's and 2's in linear time complexity

  • Checking Anagrams (check whether two string is anagrams or not)

  • Relative sorting algorithm

  • Finding subarray with given sum

  • Find the level in a binary tree with given sum K

  • Check whether a Binary Tree is BST (Binary Search Tree) or not

  • 1[0]1 Pattern Count

  • Capitalize first and last letter of each word in a line

  • Print vertical sum of a binary tree

  • Print Boundary Sum of a Binary Tree

  • Reverse a single linked list

  • Greedy Strategy to solve major algorithm problems

  • Job sequencing problem

  • Root to leaf Path Sum

  • Exit Point in a Matrix

  • Find length of loop in a linked list

  • Toppers of Class

  • Print All Nodes that don't have Sibling

  • Transform to Sum Tree

  • Shortest Source to Destination Path



Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.


make()仅制作切片,地图和通道。 make返回类型T (数据类型)的值,而不是* T

切片示例:

make([] int,10,20) –在这里,make创建切片,并根据默认数据类型值初始化其内容。 这里使用int,所以默认值为0。

new([20] int)[0:10] –在这里,它还将创建切片,但返回指向已初始化内存的指针。

示例/程序:

有两种不同的初始化映射的方法,将字符串键映射到bool值的方法如下。

 package main
import "fmt"
func main() {
// Using make() to initialize a map.
m := make ( map [ string ] bool , 0 )
fmt.Println(m)
// Using a composite literal to initialize a map.
m = map [ string ] bool {}
fmt.Println(m)
}

输出量

参考: allocation_new



最佳面试编码问题/挑战

  • 游程编码(字符串中字母的查找/打印频率)

  • 以线性时间复杂度对0、1和2的数组进行排序

  • 检查字谜(检查两个字符串是否是字谜)

  • 相对排序算法

  • 查找给定总和的子数组

  • 在给定总和K的二叉树中找到级别

  • 检查二叉树是否为BST(二叉搜索树)

  • 1 [0] 1个样式计数

  • 大写一行中每个单词的第一个和最后一个字母

  • 打印二叉树的垂直和

  • 打印二叉树的边界和

  • 反转单个链表

  • 解决主要算法问题的贪婪策略

  • 工作排序问题

  • 根到叶的路径总和

  • 矩阵中的出口点

  • 在链表中查找循环长度

  • 一流的礼帽

  • 打印所有没有兄弟的节点

  • 转换为求和树

  • 最短的源到目标路径



评论和讨论

广告:您是博主吗? 加入我们的Blogging论坛 。


翻译自: https://www.includehelp.com/golang/new-and-make-functions-with-examples.aspx

golang的new函数

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544852.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android Activity和Intent机制学习笔记

文章转自:http://www.cnblogs.com/feisky/archive/2010/01/16/1649081.html Activity Android中,Activity是所有程序的根本,所有程序的流程都运行在Activity之中,Activity具有自己的生命周期(见http://www.cnblogs.com…

scala中字符串计数_如何在Scala中创建一系列字符?

scala中字符串计数The range is a set of data from a lower value to a larger value. In Scala, we have an easy method to create a range using to keyword. 范围是从较低值到较大值的一组数据。 在Scala中,我们有一种使用to关键字创建范围的简单方法。 Synta…

多域名解析到同一网站C的php重定向代码

在index.php最前面加上以下代码&#xff1a; <?php if(strpos($_SERVER[HTTP_HOST],afish.cnblogs.com)false){#header(Location: http://afish.cnblogs.com/);echo <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org…

android 中 gravity 和 layout_gravity 的区别

文章转自&#xff1a;http://blog.csdn.net/feng88724/article/details/6333809 在进行UI布局的时候&#xff0c;可能经常会用到 android:gravity 和 android:layout_Gravity 这两个属性。 关于这两个属性的区别&#xff0c;网上已经有很多人进行了说明&#xff0c;这边再简…

线程的故事:我的3位母亲成就了优秀的我!

作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;声明&#xff1a;本故事纯属虚构&#xff0c;如果雷同那就是真事了&#xff01;大家好&#xff0c;我是线程&#xff0c;我的…

c ++查找字符串_C ++结构| 查找输出程序| 套装3

c 查找字符串Program 1: 程序1&#xff1a; #include <iostream>#pragma pack(1)using namespace std;typedef struct{int A;int B;char c1;char c2;} S;int main(){cout << sizeof(S);return 0;}Output: 输出&#xff1a; In a 32-bit system: 10In a 64-bit sy…

7种内存泄露场景和13种解决方案!

前言Java通过垃圾回收机制&#xff0c;可以自动的管理内存&#xff0c;这对开发人员来说是多么美好的事啊。但垃圾回收器并不是万能的&#xff0c;它能够处理大部分场景下的内存清理、内存泄露以及内存优化。但它也并不是万能的。不然&#xff0c;我们在实践的过程中也不会出现…

Android如何关闭Application

转载自&#xff1a;http://blog.csdn.net/hello_kevinkuang/article/details/7443005 程序启动后&#xff0c;先执行Application.onCreate()&#xff0c;再执行Activity.onCreate()。如果没有生成自己的Application&#xff0c;那么系统会为你自动生成一个。退出程序时我们一般…

Android Sqlite

2019独角兽企业重金招聘Python工程师标准>>> 一、http://lansuiyun.iteye.com/blog/1246430 good! 二、增加新表时&#xff0c;需要更新版本号。 三、Android 使用自定义cursorAdapter&#xff1a; http://blog.csdn.net/buaalei/article/details/6064792 good! 四…

优雅统计代码耗时的4种方法!

来源&#xff1a;jitwxs.cn/5aa91d10.html一、前言代码耗时统计在日常开发中算是一个十分常见的需求&#xff0c;特别是在需要找出代码性能瓶颈时。可能也是受限于 Java 的语言特性&#xff0c;总觉得代码写起来不够优雅&#xff0c;大量的耗时统计代码&#xff0c;干扰了业务逻…

小黑小波比.git clone报错解决方案

2019独角兽企业重金招聘Python工程师标准>>> zmzpzmzp1:~/data$ git clone git192.168.199.199:zmw/s910.git 正克隆到 s910... ssh: connect to host 192.168.199.199 port 22: Connection refused fatal: Could not read from remote repository.Please make sure…

Android 运行时异常 Binary XML file line # : Error inflating class

今天在做一个二维码扫描的项目的时候出现了一个错误&#xff1a; android.view.InflateException: Binary XML file line #12: Error inflating class com.zxing.view.ViewfinderView 项目里面的代码是我从以前项目里面拷贝的&#xff0c;可是各种报错&#xff0c;找了很多资…

一个sql注入直接把我们服务搞挂了

前言最近我在整理安全漏洞相关问题&#xff0c;准备在公司做一次分享。恰好&#xff0c;这段时间团队发现了一个sql注入漏洞&#xff1a;在一个公共的分页功能中&#xff0c;排序字段作为入参&#xff0c;前端页面可以自定义。在分页sql的mybatis mapper.xml中&#xff0c;orde…

reinterpret_cast和static_cast的总结

主要参考&#xff1a;http://blog.csdn.net/querw/article/details/7387594 http://www.cnblogs.com/jerry19880126/archive/2012/08/14/2638192.html http://www.cnblogs.com/ider/archive/2011/07/30/cpp_cast_operator_part3.htmlhttp://bbs.csdn.net/topics/390249118 关键…

Android实现点击两次返回键退出

转自 http://blog.sina.com.cn/s/blog_4fd2a65a0101gg2o.html 在做安卓应用是我们经常要判断用户对返回键的操作&#xff0c;一般为了防止误操作都是在用户连续按下两次返回键的时候提示用户是否退出应用程序。 第一种实现的基本原理就是&#xff0c;当按下BACK键时&#xff0c…

c#hello world_C#| 打印消息/文本(用于打印Hello world的程序)

c#hello worldTo print the message/text or any value – we use two functions: 要打印消息/文本或任何值–我们使用两个功能&#xff1a; Console.Write (); Console.Write(); This function displays text, values on the output device and does not insert a new line …

Java双刃剑之Unsafe类详解

前一段时间在研究juc源码的时候&#xff0c;发现在很多工具类中都调用了一个Unsafe类中的方法&#xff0c;出于好奇就想要研究一下这个类到底有什么作用&#xff0c;于是先查阅了一些资料&#xff0c;一查不要紧&#xff0c;很多资料中对Unsafe的态度都是这样的画风&#xff1a…

Java知多少(66)输入输出(IO)和流的概述

输入输出&#xff08;I/O&#xff09;是指程序与外部设备或其他计算机进行交互的操作。几乎所有的程序都具有输入与输出操作&#xff0c;如从键盘上读取数据&#xff0c;从本地或网络上的文件读取数据或写入数据等。通过输入和输出操作可以从外界接收信息&#xff0c;或者是把信…

单行 - JAVA 条件表达式

public class ExpressionUse{//It’s the main() function.//每个应用程序都应该有一个main()函数体。public static void main(String[] args){int a10;int b1;int c(a<b)?a:b;System.out.println("a"a);System.out.println("b"b);System.out.println…

额!Java中用户线程和守护线程区别这么大?

作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;在 Java 语言中线程分为两类&#xff1a;用户线程和守护线程&#xff0c;而二者之间的区别却鲜有人知&#xff0c;所以本文磊…