ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Go常见

2022-08-29 14:35:20  阅读:194  来源: 互联网

标签:map slice make 常见 Go new type size


GO基础语法

  1. 方法或函数调用时,传入参数一般都是值复制,除非是map、slice、channel、指针类型是引用传递

  2. 短的变量声明(Short Variable Declarations),即自动推导,只能在函数内部使用

  3. 字符串与[]byte之间的转换是复制(有内存消耗),使用range来避免内存分配来提高性能

  4. 使用for range迭代map时每次迭代的顺序可能不一样,map的迭代事随机的

  5. switch case匹配case条件后默认退出,除非使用fallthrough继续匹配

  6. 没有前置自增、前置自减

  7. 位运算优先级高于四则运算

  8. log包中的log.Fatallog.Panic不仅仅记录在日志,还会终止程序

  9. nil只能赋值给指针、channel、func、interface、map或slice类型的变量

  10. makenew的区别

    1. make只能用于slice、map、channel的初始化,它返回的类型就是这三个类型的本身,而不是他们的指针类型,因为这三种类型就是引用类型。分配空间并初始化。

      // 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
      
    2. new只接受一个参数,这个参数是一个类型,返回一个指向该类型内存地址的指针,同时把分配的内存置为零。new不仅能够为系统默认的数据类型分配空间,也能为自定义类型分配空间

      // 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
      
  11. 常量无法寻址,不能进行取指针操作操作

标签:map,slice,make,常见,Go,new,type,size
来源: https://www.cnblogs.com/lxuegod/p/16635808.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有