Go常见
2024-10-20 13:51:24
GO基础语法
方法或函数调用时,传入参数一般都是值复制,除非是map、slice、channel、指针类型是引用传递
短的变量声明(Short Variable Declarations),即自动推导,只能在函数内部使用
字符串与
[]byte
之间的转换是复制(有内存消耗),使用range来避免内存分配来提高性能使用
for range
迭代map
时每次迭代的顺序可能不一样,map
的迭代事随机的switch case
匹配case
条件后默认退出,除非使用fallthrough
继续匹配没有前置自增、前置自减
位运算优先级高于四则运算
log包中的
log.Fatal
和log.Panic
不仅仅记录在日志,还会终止程序nil
只能赋值给指针、channel、func、interface、map或slice类型的变量make
和new
的区别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
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
常量无法寻址,不能进行取指针操作操作
最新文章
- 嵌入式:J-link刷固件(坑)
- eclipse不显示Android SDK Manager标签
- CentOS(RedHat)命令行永久修改IP地址、网关、DNS
- 使用WPF动态生成Code 39条形码
- 在source insight中集成astyle
- 设置type为file的input标签选择图片类型
- https SSL主流数字证书都有哪些格式?
- Centos环境下部署游戏服务器-简介
- VS2008 工程中部分文件不参与编译 从生成中排除【Worldsing笔记】
- 数据库中DDL、DML、DCL和TCP概念
- Office2007设置无格式粘贴
- 经典 SQL
- Hadoop之运行模式
- [原]shell批量文件增删改前后缀
- vue权威指南笔记01——样式的设置
- 日常英语---十二、MapleStory/Monsters/Level 1-10(Horny Mushroom)
- Hadoop基础-常见异常剖析之防坑小技巧
- Java循环中标签的作用(转)
- c++ 分配与释放内存
- java实验2实验报告(20135232王玥)
热门文章
- Maven配置【详细】
- Node.js精进(7)——日志
- HiEngine:可媲美本地的云原生内存数据库引擎
- CentOS7下bash升级
- JTable和MVC设计模式
- 2022.7.19 $\mathcal{S\,\,T\,L}$
- MPI简谈
- iOS自动化打包 Jenkins+Gitlab+Fastlane+蒲公英+钉钉
- 在Ubuntu下编译安装GreatSQL
- http、https和Cookie