GO语言
syouya.shiraki
这个作者很懒,什么都没留下…
展开
-
Go语言入门-原子操作
Go语言入门-原子操作什么是原子操作参考笔者博文 原子操作:CAS、TAS、TTAS、FAA浅析Go语言中的atomic包Atomaic包中提供了原子的操作包括:数据读取类函数 LoadXXX()数据写入类函数 StoreXXX()数据自增(FFA)类函数 AddXXX()数据交换类函数 SwapXXX()比较并交换(CAS)函数 CompareAndSwapXXX()方法列表// SwapInt32 atomically stores new into *addr and retu原创 2020-07-11 15:37:14 · 640 阅读 · 0 评论 -
Go语言入门-并发
Go语言入门-并发什么是并发1. 并发:在同一个时间段能够执行多个任务,把时间段分解为时间片,在不同的时间片通过状态切换来执行不同任务。(单个cpu)提到并发,就得提出另一个相关联的概念并行2. 并行:在同一个时间点执行的任务有多个。(多个cpu的情况)多核情况下并发和并行是可以组合的。而Go语言的并发goroutine也就是协程机制,像java中并发使用的是线程池也就是线程。至于线程和协程的区别。可以从以下进行区分:协程:独立的、更小的栈空间、共享堆、调度可以在用户态控制、没有优先级、没有编原创 2020-07-10 00:03:34 · 364 阅读 · 2 评论 -
Go语言入门-反射
Go语言入门-反射概述定义Reflection in computing is the ability of a program to examine its own structure, particularly through types; it’s a form of metaprogramming. It’s also a great source of confusion.反射(Reflection )在运行时程序检查其自身结构的能力,特别是常用于类型中,能让程序在运行时检查和获取对象原创 2020-06-22 21:32:01 · 383 阅读 · 0 评论 -
Go语言入门-接口
Go语言入门-接口概述定义An interface type specifies a method set called its interface. A variable of interface type can store a value of any type with a method set that is any superset of the interface. Such a type is said to implement the interface. The value of原创 2020-06-16 21:08:05 · 664 阅读 · 0 评论 -
Go语言入门-方法
Go语言入门-方法概念A method is a function with a receiver. A method declaration binds an identifier, the method name, to a method, and associates the method with the receiver’s base type.方法是一种特殊的函数,绑定一个receiver。 与接收者的基本类型关联。基本语法--官方描述MethodDecl = "func" Re原创 2020-06-10 15:45:25 · 489 阅读 · 0 评论 -
Go语言入门-struct结构
Go语言入门-struct结构关键字typeGo语言中使用type关键字自定义类型。The new type is called a defined type. It is different from any other type, including the type it is created from.语法TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) .TypeSpec = AliasDecl | TypeDef原创 2020-06-04 00:46:46 · 672 阅读 · 0 评论 -
Go语言入门-错误处理
Go语言入门-错误处理错误到底是什么?错误在go中是一个接口类型,便于自定义错误扩展。// 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}创建第一个错误变量标准库创建错误变量原创 2020-06-01 21:27:39 · 324 阅读 · 0 评论 -
Go语言入门-异常处理-pannic和recover
Go语言入门-异常处理-pannic和recover简述Go语言try…catch结构化的异常机制,但是可以使用panic进行错误的抛出,使用recover来进行异常恢复处理。基础语法func panic(interface{})func recover() interface{}使用场景Go语言函数中可以抛出一个运行时恐慌后,然后通过defer使用recover来处理运行时恐慌。panic和recover是内置函数。而不是语句。由于panic参数是空接口类型,因此可使用任何对子昂作为原创 2020-06-01 20:26:47 · 1188 阅读 · 0 评论 -
Go语言入门-延迟调用-defer
Go语言入门-延迟调用-defer定义语句defer想当前函数注册一个稍微执行的函数调用,它会在主调函数或者方法返回之前但是其返回值(当返回值存在)计算后执行。存在多个defer语句采用后进先出LIFO(Last In First Out)的顺序执行主要用于释放资源、解出锁定、以及错误处理等场景。实例1简单的展示defer函数调用func main() { defer fmt.Println("end") fmt.Println("start")}/**output:start原创 2020-05-13 23:59:28 · 386 阅读 · 0 评论 -
Go语言入门-函数function类型探索
Go语言入门-函数-function定义Go语言中函数面向过程编程中最小的模块单元,具备输入、输出的功能,完成从输入到输出的转换,主要是把业务逻辑或者算法分解成具备单一功能,独立解耦的程序块。函数签名函数的签名包括:关键字 “func”、函数名称、函数参数、函数返回值。Go语言的函数定义格式://多个返回值,需要用括号包裹起来func functionName([parameterList]) ([returnTypes]) { ... body ...}//返原创 2020-05-12 21:39:15 · 703 阅读 · 0 评论 -
Go语言入门-常用数据结构之字典-map
Go语言入门-常用数据结构之字典-map定义概述A map is an unordered group of elements of one type, called the element type, indexed by a set of unique keys of another type, called the key type. The value of an uninitialized map is nil.–字典是一种类型的无序元素组,称为元素类型,由唯一键(称为键类型)索引关联其他原创 2020-05-11 18:09:42 · 712 阅读 · 0 评论 -
Go语言入门-常用数据结构之切片slice
Go语言入门-常用数据结构之切片slice定义概述A slice is a descriptor for a contiguous segment of an underlying array and provides access to a numbered sequence of elements from that array.The number of elements is called the length of the slice and is never negative. The原创 2020-05-10 22:31:19 · 443 阅读 · 0 评论 -
Go语言入门-常用数据结构之数组
Go语言入门-常用数据结构之数组定义概述An array is a numbered sequence of elements of a single type, called the element type. The number of elements is called the length of the array and is never negative.-数组是单一类型元素(元素类型)组成并且被编号序列。 元素的数量称为数组的长度,绝不能为负。这句话体现数组的几个特点:数组中原创 2020-05-10 11:16:44 · 715 阅读 · 0 评论 -
goland格式化代码快捷键
goland格式化代码快捷键win快捷键:ctrl + alt +I其他OS或者修改过快键键。可以通过设置中查找具体路径:settings->Keymap->输入 indent搜索->选择code下Auto-Indent Lines 查找指定快捷键。原创 2020-05-09 16:33:26 · 10232 阅读 · 0 评论 -
Go语言select、switch中使用break有用吗?
Go语言select、switch中使用break有用吗?问题-select中使用break有用吗?直接上select和break的示例func test1() { select { case <-time.After(time.Second): fmt.Println("一秒后执行") //break default: }}func test1() { select { case <-time.Aft原创 2020-05-09 14:37:42 · 8650 阅读 · 2 评论 -
Go语言入门-流程控制(for、switch、select、range、break、goto、continue)
Go语言入门-流程控制条件语句 if语句 if语句语法if bool表达式 { //左花括号必须跟随bool表达式 //当bool表达式为true是执行。 Todo....}示例如下:package mainfunc main() { a := 10 if a >= 10 { println("a 大于等于10") //10 } if a <100 { println("a小于等于100") //100 }}/**ou原创 2020-05-08 23:46:14 · 882 阅读 · 1 评论 -
GO语言入门-运算符
GO语言入门-运算符目录GO语言入门-运算符概述:分类:算数运算符关系运算符号逻辑运算符位运算符赋值运算符优先级自增自减概述:go语言内置的运算符号可以分为算数运算符、关系运算符、逻辑运算符、位运算符、赋值运算符。全部的运算符如下:全部运算符号及分隔符列表(来自《go语言学习笔记》) + & += &= ...原创 2020-05-08 12:59:58 · 457 阅读 · 0 评论 -
Go语言入门-关键词和命名方式
go语言入门-关键字和命名方式关键字go语言只有25个关键词break default func interface select case defer go map struct chan else goto package switch const fal...原创 2020-05-08 10:30:31 · 297 阅读 · 0 评论 -
Go语言入门-类型
go语言入门-类型基本类型基础数据类型 类型、长度、默认值、说明介绍字面值常量引用类型未命名类型类型转换类型长度 (单位Byte)默认值说明bool1falsebyte10同uint8int4/80默认的整数类型,具体位数取决于平台,32bit的OS长度为4,64位OS长度为8uint4/80无符号整数,具体位数取决于平台,32bit的OS长度为4,64位OS长...原创 2020-05-07 19:15:44 · 465 阅读 · 0 评论 -
go语言入门-常量补充整型最大值最小值
go语言入门-常量补充整型最大值最小值引言go 语言中没有整型最大值最小值常量,查看java源码看以看出是常量值是写死在代码中,如下 @Native public static final int MIN_VALUE = 0x80000000; /** * A constant holding the maximum value an {@code int} c...原创 2020-04-30 15:43:20 · 680 阅读 · 0 评论 -
go语言入门-常量(const和itoa)
go语言入门-常量定义常量:常量标识恒定不变的值,区别于变量var。var-变量不赋值存在默认值,但是常量声明是必须显示赋值。常量关键字:const常量不能使用 “:=” 语法定义。声明方式普通声明方式const E float64 = 1.602const EE = 1.602 // 类型可以省略批量声明方式常用声明使用方式const ( A = 'A' B = 'B...原创 2020-04-29 18:04:14 · 2464 阅读 · 0 评论 -
go语言入门-变量
go语言入门-变量变量声明的方式普通声明方式-不赋值var name string //""var score float32 //0.0fvar isFail bool //false批量声明方式-不复制var ( name string //"" score float32 //0.0f isFail bool //false)不管是批量声明还是普通声明方式...原创 2020-04-28 00:50:59 · 245 阅读 · 0 评论 -
GO语言变量作用域-坑记录
GO语言变量作用域-坑记录1、main包的全局变量,其他文件无法使用。解决办法:全局变量放置在其他包中进行引用2. 同名变量局部变量优先于全局变量,但是在使用 := 进行类型推导时会默认创建一个局部变量,该局部变量若与全局变量同名,则不会影响全局变量值。解决办法:尽量避免直接使用全局变量,使用函数封装。 少用 := 类型推导式示例代码如下:package main...原创 2019-12-05 10:43:29 · 556 阅读 · 1 评论