golang new与make的一点区别理解
方法注释可以看出
1、new可以实力化任何类型,返回一个类型的指针,指针指向该类型的零值
slice:= new([]int)
可以直接使用slice01 := append(*slice,1,2,3)
map := new(map[int]string) 貌似不可以直接使用 map 指针指向nil *map == nil 为true
2、make 值可以实力化slice,map,channal三种类型,返回这三种类新的实例化对象本身,可以直接使用
具体区别可以看源码注释
通过new的 slice 之所以能用,应该是append引起的底层数组重新分配了,因为append会判断cap的容量,如果容量不够会重新分配底层数组,cap方法即使slice为nil,cap的值为零
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file./*Package builtin provides documentation for Go's predeclared identifiers.The items documented here are not actually in package builtinbut their descriptions here allow godoc to present documentationfor the language's special identifiers.
*/
package builtin// bool is the set of boolean values, true and false.
type bool bool// true and false are the two untyped boolean values.
const (true = 0 == 0 // Untyped bool.false = 0 != 0 // Untyped bool.
)// uint8 is the set of all unsigned 8-bit integers.
// Range: 0 through 255.
type uint8 uint8// uint16 is the set of all unsigned 16-bit integers.
// Range: 0 through 65535.
type uint16 uint16// uint32 is the set of all unsigned 32-bit integers.
// Range: 0 through 4294967295.
type uint32 uint32// uint64 is the set of all unsigned 64-bit integers.
// Range: 0 through 18446744073709551615.
type uint64 uint64// int8 is the set of all signed 8-bit integers.
// Range: -128 through 127.
type int8 int8// int16 is the set of all signed 16-bit integers.
// Range: -32768 through 32767.
type int16 int16// int32 is the set of all signed 32-bit integers.
// Range: -2147483648 through 2147483647.
type int32 int32// int64 is the set of all signed 64-bit integers.
// Range: -9223372036854775808 through 9223372036854775807.
type int64 int64// float32 is the set of all IEEE-754 32-bit floating-point numbers.
type float32 float32// float64 is the set of all IEEE-754 64-bit floating-point numbers.
type float64 float64// complex64 is the set of all complex numbers with float32 real and
// imaginary parts.
type complex64 complex64// complex128 is the set of all complex numbers with float64 real and
// imaginary parts.
type complex128 complex128// string is the set of all strings of 8-bit bytes, conventionally but not
// necessarily representing UTF-8-encoded text. A string may be empty, but
// not nil. Values of string type are immutable.
type string string// int is a signed integer type that is at least 32 bits in size. It is a
// distinct type, however, and not an alias for, say, int32.
type int int// uint is an unsigned integer type that is at least 32 bits in size. It is a
// distinct type, however, and not an alias for, say, uint32.
type uint uint// uintptr is an integer type that is large enough to hold the bit pattern of
// any pointer.
type uintptr uintptr// byte is an alias for uint8 and is equivalent to uint8 in all ways. It is
// used, by convention, to distinguish byte values from 8-bit unsigned
// integer values.
type byte = uint8// rune is an alias for int32 and is equivalent to int32 in all ways. It is
// used, by convention, to distinguish character values from integer values.
type rune = int32// iota is a predeclared identifier representing the untyped integer ordinal
// number of the current const specification in a (usually parenthesized)
// const declaration. It is zero-indexed.
const iota = 0 // Untyped int.// nil is a predeclared identifier representing the zero value for a
// pointer, channel, func, interface, map, or slice type.
var nil Type // Type must be a pointer, channel, func, interface, map, or slice type// Type is here for the purposes of documentation only. It is a stand-in
// for any Go type, but represents the same type for any given function
// invocation.
type Type int// Type1 is here for the purposes of documentation only. It is a stand-in
// for any Go type, but represents the same type for any given function
// invocation.
type Type1 int// IntegerType is here for the purposes of documentation only. It is a stand-in
// for any integer type: int, uint, int8 etc.
type IntegerType int// FloatType is here for the purposes of documentation only. It is a stand-in
// for either float type: float32 or float64.
type FloatType float32// ComplexType is here for the purposes of documentation only. It is a
// stand-in for either complex type: complex64 or complex128.
type ComplexType complex64// The append built-in function appends elements to the end of a slice. If
// it has sufficient capacity, the destination is resliced to accommodate the
// new elements. If it does not, a new underlying array will be allocated.
// Append returns the updated slice. It is therefore necessary to store the
// result of append, often in the variable holding the slice itself:
// slice = append(slice, elem1, elem2)
// slice = append(slice, anotherSlice...)
// As a special case, it is legal to append a string to a byte slice, like this:
// slice = append([]byte("hello "), "world"...)
func append(slice []Type, elems ...Type) []Type// The copy built-in function copies elements from a source slice into a
// destination slice. (As a special case, it also will copy bytes from a
// string to a slice of bytes.) The source and destination may overlap. Copy
// returns the number of elements copied, which will be the minimum of
// len(src) and len(dst).
func copy(dst, src []Type) int// The delete built-in function deletes the element with the specified key
// (m[key]) from the map. If m is nil or there is no such element, delete
// is a no-op.
func delete(m map[Type]Type1, key Type)// The len built-in function returns the length of v, according to its type:
// Array: the number of elements in v.
// Pointer to array: the number of elements in *v (even if v is nil).
// Slice, or map: the number of elements in v; if v is nil, len(v) is zero.
// String: the number of bytes in v.
// Channel: the number of elements queued (unread) in the channel buffer;
// if v is nil, len(v) is zero.
// For some arguments, such as a string literal or a simple array expression, the
// result can be a constant. See the Go language specification's "Length and
// capacity" section for details.
func len(v Type) int// The cap built-in function returns the capacity of v, according to its type:
// Array: the number of elements in v (same as len(v)).
// Pointer to array: the number of elements in *v (same as len(v)).
// Slice: the maximum length the slice can reach when resliced;
// if v is nil, cap(v) is zero.
// Channel: the channel buffer capacity, in units of elements;
// if v is nil, cap(v) is zero.
// For some arguments, such as a simple array expression, the result can be a
// constant. See the Go language specification's "Length and capacity" section for
// details.
func cap(v Type) int// 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// The complex built-in function constructs a complex value from two
// floating-point values. The real and imaginary parts must be of the same
// size, either float32 or float64 (or assignable to them), and the return
// value will be the corresponding complex type (complex64 for float32,
// complex128 for float64).
func complex(r, i FloatType) ComplexType// The real built-in function returns the real part of the complex number c.
// The return value will be floating point type corresponding to the type of c.
func real(c ComplexType) FloatType// The imag built-in function returns the imaginary part of the complex
// number c. The return value will be floating point type corresponding to
// the type of c.
func imag(c ComplexType) FloatType// The close built-in function closes a channel, which must be either
// bidirectional or send-only. It should be executed only by the sender,
// never the receiver, and has the effect of shutting down the channel after
// the last sent value is received. After the last value has been received
// from a closed channel c, any receive from c will succeed without
// blocking, returning the zero value for the channel element. The form
// x, ok := <-c
// will also set ok to false for a closed channel.
func close(c chan<- Type)// The panic built-in function stops normal execution of the current
// goroutine. When a function F calls panic, normal execution of F stops
// immediately. Any functions whose execution was deferred by F are run in
// the usual way, and then F returns to its caller. To the caller G, the
// invocation of F then behaves like a call to panic, terminating G's
// execution and running any deferred functions. This continues until all
// functions in the executing goroutine have stopped, in reverse order. At
// that point, the program is terminated with a non-zero exit code. This
// termination sequence is called panicking and can be controlled by the
// built-in function recover.
func panic(v interface{})// The recover built-in function allows a program to manage behavior of a
// panicking goroutine. Executing a call to recover inside a deferred
// function (but not any function called by it) stops the panicking sequence
// by restoring normal execution and retrieves the error value passed to the
// call of panic. If recover is called outside the deferred function it will
// not stop a panicking sequence. In this case, or when the goroutine is not
// panicking, or if the argument supplied to panic was nil, recover returns
// nil. Thus the return value from recover reports whether the goroutine is
// panicking.
func recover() interface{}// The print built-in function formats its arguments in an
// implementation-specific way and writes the result to standard error.
// Print is useful for bootstrapping and debugging; it is not guaranteed
// to stay in the language.
func print(args ...Type)// The println built-in function formats its arguments in an
// implementation-specific way and writes the result to standard error.
// Spaces are always added between arguments and a newline is appended.
// Println is useful for bootstrapping and debugging; it is not guaranteed
// to stay in the language.
func println(args ...Type)// The error built-in interface type is the conventional interface for
// representing an error condition, with the nil value representing no error.
type error interface {Error() string
}
golang new与make的一点区别理解相关推荐
- Golang中WaitGroup使用的一点坑
Golang中WaitGroup使用的一点坑 Golang 中的 WaitGroup 一直是同步 goroutine 的推荐实践.自己用了两年多也没遇到过什么问题.直到一天午睡后,同事扔过来一段奇怪的 ...
- AngularJS』一点小小的理解
『AngularJS』一点小小的理解 AngularJS 是一个前端的以Javascript为主的MVC框架.与AngularJS相类似的还有EmberJS. 随着时代在进步,各种各样的开发理念与开发 ...
- 事件与委托的一点概念理解
事件与委托的一点概念理解 早就接触过delegate和event,但是始终是一知半解,今天刚好有机会就好好研究一下! Delegate 顾名思义就是委托,委托的意思就是麻烦别人帮我干些事情.比如,我要 ...
- golang值类型与引用类型的区别
1.golang值类型与引用类型的区别 1.1 本质存储区别 值类型与引用类型的区别主要在于: 值类型的变量: 存储当前类型的存储的数据.值类型包括数值类型.bool.string.数组.struct ...
- Layui的一点小理解(上)
首先声明这是个人的一点理解,如有不对之处请指正,以下的例子有在官网上看到的,有的是自己写的.还是老规矩最后会附上官网的,如有不明白之处,请查看文档或留言. 既然说Layui,当然要简单的介绍以下什么是 ...
- 关于Ndis驱动的一点个人理解
对写过驱动的一些总结: 1,TDI层驱动,该层驱动位于windows kernel网络驱动的最顶层,个人觉得主要是针对连接时的redirect,filter比较好使,比如限制本地一些端口不能往外连接, ...
- Vivado IP核的综合模式:Global以及Out of context的区别理解
提出问题 从接触FPGA生成第一个IP核开始,就遇到一个选择的问题,定制完成IP核后,最有一页会有一个选择综合模式的问题,有两个选择,一个是Global,另一个是Out of Context,是什么意 ...
- flatmap 与map 的区别 java_map和flatmap的区别+理解、学习与使用 Java 中的 Optional
orElse() 和 orElseGet()的不同之处 乍一看,这两种方法似乎起着同样的作用.然而事实并非如此.我们创建一些示例来突出二者行为上的异同. 我们先来看看对象为空时他们的行为: @Test ...
- java抽象和接口的理解_Java接口实现与抽象类的区别理解 | 彬菌
版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创! 恰饭广告 接口:接口是抽象方法的集合.接口通常以interface来声明. 接口注意事项: 1.严格来说,接口是用来实现(i ...
最新文章
- android ValueAnimator学习
- DataGrid中添加DropdownList时的数据绑定
- java集合类详解和使用_Java 集合类详解
- C++对象的赋值和复制
- sass报 error (Line XX: Invalid GBK character \xE4) 的解决办法
- Could not find the main class: org.apache.catalina.startup.Bootstrap. Program will exit.
- 猪肉都被绑上了“家族标签”,大数据已波及到农牧业!
- jackson 驼峰注解_jackson 常用注解,比如忽略某些属性,驼峰和下划线互转
- 学python买什么书-关于 Python 的经典入门书籍有哪些?
- http之url和uri
- tomcat下的公共jar包配置
- wait()、notify()、notifyAll()原理用法详解sleep()与wait()区别
- Linux 下串口编程入门
- 隔离模块介绍-隔离电源模块
- 【算法-LeetCode】121. 买卖股票的最佳时机(动态规划;贪心)
- Nicholas C. Zakas谈怎样才能成为优秀的前端工程师
- 生成自己的浏览器证书
- 超棒的电烙铁的焊接方法图解
- 绘制热力图seaborn.heatmap,cmap设置颜色的参数
- java群侠传_梦幻群侠传之天剑如虹(下)
热门文章
- linux遍历目录漏洞,Mutiny 多个目录遍历漏洞(CVE-2013-0136)
- java修改另存excel_Word文档导入Excel总变形,调整格式你花了1小时,同事三步就搞定...
- vue3——ref reactive函数
- sorted是python的内置函数吗_Python中的内置sorted()函数
- 前台setcookie之后从后台取出来_后台设置Cookie值,前台进行获取
- 《代码敲不队》第三次作业:团队项目的原型设计
- 友盟页面访问路径全量统计功能上线啦!
- JQuery + XML作为前后台数据交换格式实践
- Android应用程序组件Content Provider的共享数据更新通知机制分析
- 诺微联盟催生智能手机的三足鼎立