首页 > 其他分享 >Go常见

Go常见

时间:2022-08-29 14:38:26浏览次数:51  
标签: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
From: https://www.cnblogs.com/lxuegod/p/16635808.html

相关文章

  • Django使用Redis进行缓存详细流程
    1.背景和意义服务器数据非经常更新。若每次都从硬盘读取一次,浪费服务器资源、拖慢响应速度。而且数据更新频率较高,服务器负担比较大。若保存到数据库,还需要额外建立一张对......
  • tiangolo/uvicorn-gunicorn:python3.8 fastapi docker部署
    经验教训:1、gunicornlog文件固定放置在容器中目录:/code/logs,所以这个目录必需映射出来;2、按照fastapi官方建议,uvicorn运行目录:/app,所以这个目录要映射到代码目录;3、......
  • django2.x -- 报错"UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in posi
    解决方法:进入debug.py文件将331行代码修改编码方式为‘utf-8’......
  • go语言文件处理-02
    go语言zip归档文件的读写操作Go语言的标准库提供了对几种压缩格式的支持,其中包括gzip,因此Go程序可以无缝地读写.gz扩展名的gzip压缩文件或非.gz扩展名的非压缩文......
  • axios的特点 - 支持多种请求方式 - 常见的配置选项
    1.特点:1.在浏览器中发送XMLHttpRequest请求2.在node.js中发送http请求3.支持PromiseAPI4.拦截请求和响应 2.支持多种请求方式:axios(c......
  • 刷题篇。树、图、数组等相关常见处理操作
    一、树 序号做什么方法伪码 1前、中、后遍历    2以任意节点为根重构树    ......
  • Dragonfly 基于 P2P 的文件和镜像分发系统
    作者:孙景文、吴迪背景网络下载提起网络下载领域,你应该首先会想到基于TCP/IP协议簇的C/S模式。这种模式希望每一个客户机都与服务器建立TCP连接,服务器轮询监听TC......
  • Golang 中反射的应用与理解
    Golang中反射的应用与理解https://mp.weixin.qq.com/s/TmzV2VTfkE8of2_zuKa0gAGolang中反射的应用与理解原创 赵燕辉 字节跳动技术团队 2022-08-2312:00 发表于......
  • golang for range
    m:=make(map[int]*int)arr:=[]int{1,2,3,4,5}fori,v:=rangearr{m[i]=&v}fork,v:=rangem{fmt.Println(......
  • go语言文件处理-01
    go语言json文件的读写操作JSON是一种使用UTF-8编码的纯文本格式,采用完全独立于语言的文本格式,由于写起来比XML格式方便,并且更为紧凑,同时所需的处理时间也更少,致使JS......