golang

(二十六)golang--切片

好久不见. 提交于 2019-12-05 02:41:53
基本介绍: 切片是数组的引用; 切片的使用和数组类似; 切片的长度是可以变化的; 切片的定义 var a []int,注意和数组定义的区别; 切片不仅可以使用len函数,还有cap函数来计算切片的容量; 切片在内存中的形式: 从该图我们可以得出: slice的确是一个引用类型; slice从底层来说,其实就是一个数据结构struct: type slice sruct {   ptr *[2]int   len int   cap int } 切片使用的三种方式: 即上面的对数组的操作; 通过make来创建切片;var slice []int = make([]int,4,20)三个参数分别是类型,长度,容量 定义一个切片,直接就指定数组;var slice []int = []int{1,3,5} 方式1和方式2的区别: 方式1是直接引用数组,这个数组是事先存在的,程序员是可见的;方式2通过make,make也会创建一个数组,是由切片在底层进行维护,对程序员是不可见的; 切片的遍历:与数组一样,也是有两种; 切片使用的注意事项: 切片定义后需要引用到一个数组或者make一个空间供切片使用; 切片之后可以继续切片; append内置函数,可以动态增加切片; var slice []int = []int{1,2,3} slice = append(slice,4,5,6)

Golang time包使用

a 夏天 提交于 2019-12-05 02:02:51
time 包使用 一个printf的使用方式 a := 1 fmt.Printf( "%02d" ,a) %cNd,表示对齐N位,不够的用字符c补齐 Go Time 的使用 1.format形式 获取年月日方法 t := time .Now() y := t.Year() // 年 m := int(t.Month()) //月 d := t.Day( )// 日 h := t.Hour() //小时 min := t.Minute( )//分钟 s := t.Second( )//秒 fmt.Printf( "%d-%d-%d" ,y,m,d )// 2017-12-26 fmt.Printf( "%d%02d%02d" ,y,m,d) // 20180101 获取格式为:2017-12-29 16:58:39 t := time .Now () fmt .Println (t .String ()[: 19 ]) layout := "2006-01-02 15:04:05" fmt .Println (t .Format (layout)) 获取三个小时之前的时间 du, _ := time .ParseDuration ( "-3h" ) t := time .Now () .Add (du) 获取三天之前的时间 t := time .Now () t .AddDate (

golang_定时器: time包中Timer类型函数的用法介绍

放肆的年华 提交于 2019-12-05 02:02:36
Timer 1. type Timer type Timer struct { C <- chan Time // 内含隐藏或非导出字段 } Timer类型代表单次时间事件。当Timer到期时,当时的时间会被发送给C,除非Timer是被AfterFunc函数创建的。 time 组成 time.Duration(时间长度,消耗时间) time.Time(时间点) time.C(放时间的channel通道)(注:Time.C:=make(chan time.Time)) 2. time.NewTimer func NewTimer(d Duration) *Timer NewTimer创建一个Timer,它会在最少过去时间段d后到期,向其自身的C字段发送当时的时间。 time.NewTimer(时间间隔n):在指定的时间n后,系统自动将当前时间写入到Time结构体中的通道中 myTimer := time . NewTimer ( time . second ) //1秒以后将时间写入time通道中 <- myTimer . C //读取通道中的时间 fmt . Println ( "timed out" ) 3. time.Sleep func Sleep(d Duration) Sleep阻塞当前go程至少d代表的时间段。d<=0时,Sleep会立刻返回。 time . Sleep

golang time操作整理

烂漫一生 提交于 2019-12-05 02:02:15
这里的内容大部分是从他人处整理得到 http://my.oschina.net/1123581321/blog/190942 时间戳 当前时间戳 fmt.Println(time.Now().Unix()) # 1389058332 str格式化时间 当前格式化时间 fmt.Println(time.Now().Format("2006-01-02 15:04:05")) # 这是个奇葩,必须是这个时间点, 据说是go诞生之日, 记忆方法:6-1-2-3-4-5 # 2014-01-07 09:42:20 时间戳转str格式化时间 str_time := time.Unix(1389058332, 0).Format("2006-01-02 15:04:05") fmt.Println(str_time) # 2014-01-07 09:32:12 str格式化时间转时间戳 这个比较麻烦 the_time := time.Date(2014, 1, 7, 5, 50, 4, 0, time.Local) unix_time := the_time.Unix() fmt.Println(unix_time) # 389045004 还有一种方法,使用time.Parse the_time, err := time.Parse("2006-01-02 15:04:05", "2014

Golang 须知知识点

流过昼夜 提交于 2019-12-05 01:58:28
(1)同级目录不能放多个包,否则报编译错误; getLocalIP.go:8:5: found packages test (test.go) and test1 (test1.go) in /data/goTest/src/getLocalIP/test (2)实现一个接口需要实现接口中的所有方法。 (3)Golang中根据首字母的大小写来确定可以访问的权限。方法名、结构体名、结构体成员名、常量、变量名等,如果首字母大写,则可以被导出,即可以被其他的包访问;如果首字母小写,则不可导出,只能在本包中使用。可以简单的理解成,首字母大写是公有的,首字母小写是私有的; (4)import写的到底是目录名还是包名,包名是不是必须和目录名一致? 结论:import的时候写的是目录名,引用的时候写的是包名。包名不必和目录名一致,但为了更好的维护和更高的可读性,普遍的做法是包名和目录名一致。 (5)cannot assign to struct field xxx in map。 Golang中无法在map中给struct成员赋值。比如: package main import “fmt” var m = map[string]struct{x, y int} { “foo”: {2, 3}, } func main() { m[“foo”].x = 4 // cannot assign to

Golang 的类型与零值

允我心安 提交于 2019-12-05 01:57:58
Golang 中定义不同类型的变量,不是通过声明就是通过 make 或 new 。 未显式初始化时,将被赋予一个默认值,该默认值便为该类型的零值。不同的类型有不同的零值。 类型 类型名 零值 字符类型 byte, rune 0 数值类型 float32, float64, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, complex64, complex128 0 布尔类型 bool false 字符串 sting “”(空字符串) slice []TYPE nil map map[TYPE]TYPE nil 指针 uintptr nil 函数 func nil 接口 interface nil 信道 chan nil 其中,TYPE 表示具体的类型。 示例代码: package main import ( "fmt" ) type Student struct { Sno int //整型(数值类型) Name string //字符串 Gender bool //布尔类型 Age float32 //浮点型(数值类型) } type StudentPrint interface { Print() } func main() { stu := new(Student) fmt

在 go/golang语言中使用 google Protocol Buffer

佐手、 提交于 2019-12-05 01:57:45
怎么在go语言中实用google protocol Buffer呢? 现在的潮流趋势就是一键搞定,跟ubuntu安装软件一样 go get code.google.com/p/goprotobuf/{proto,protoc-gen-go} go install code.google.com/p/goprotobuf/proto 搞定,可以在 $GO_PATH/bin下找到 protoc-gen-go 这个程序,那么就可以实用protoc-gen-go 进行go语言的proto文件的自动生成了。 go1.0 使用: protoc-gen-go --go_out=. hellowrold.proto go1.1 直接实用以下命令 protoc --go_out=. hellowrold.proto proto文件: package lm; message helloworld { required int32 id = 1 ; // ID required string str = 2 ; // str optional int32 opt = 3 ; // optional field } package lm; 因此必须放到lm目录下(参考proto规范) ,在lm下面使用命令生成文件 protoc-gen-go --go_out=. hellowrold.proto

深入Golang之sync.Pool详解

妖精的绣舞 提交于 2019-12-05 01:57:19
我们通常用golang来构建高并发场景下的应用,但是由于golang内建的GC机制会影响应用的性能,为了减少GC,golang提供了对象重用的机制,也就是sync.Pool对象池。 sync.Pool是可伸缩的,并发安全的。其大小仅受限于内存的大小,可以被看作是一个存放可重用对象的值的容器。 设计的目的是存放已经分配的但是暂时不用的对象,在需要用到的时候直接从pool中取。 任何存放区其中的值可以在任何时候被删除而不通知,在高负载下可以动态的扩容,在不活跃时对象池会收缩。 sync.Pool首先声明了两个结构体 // Local per-P Pool appendix. type poolLocalInternal struct { private interface{} // Can be used only by the respective P. shared []interface{} // Can be used by any P. Mutex // Protects shared. } type poolLocal struct { poolLocalInternal // Prevents false sharing on widespread platforms with // 128 mod (cache line size) = 0 . pad [128 -

Golang sync.Cond 简介与用法

核能气质少年 提交于 2019-12-05 01:55:10
1.简介 sync.Cond 实现了一个条件变量,在 Locker 的基础上增加了一个消息通知的功能,其内部维护了一个等待队列,队列中存放的是所有等待在这个 sync.Cond 的 Go 程,即保存了一个通知列表。sync.Cond 可以用来唤醒一个或所有因等待条件变量而阻塞的 Go 程,以此来实现多个 Go 程间的同步。sync.Cond 的定义及成员函数如下: type Cond struct { // L is held while observing or changing the condition L Locker // contains filtered or unexported fields } //创建一个带锁的条件变量,Locker 通常是一个 *Mutex 或 *RWMutex func NewCond(l Locker) *Cond //唤醒所有因等待条件变量 c 阻塞的 goroutine func (c *Cond) Broadcast() //唤醒一个因等待条件变量 c 阻塞的 goroutine func (c *Cond) Signal() //自动解锁 c.L 并挂起 goroutine。只有当被 Broadcast 和 Signal 唤醒,Wait 才能返回,返回前会锁定 c.L func (c *Cond) Wait() 注意:在调用

golang的临时对象池sync.Pool

≡放荡痞女 提交于 2019-12-05 01:53:59
今天在写码之时,发现了同事用到了sync.pool。因不知其因,遂Google之。虽然大概知道其原因和用法。还不能融汇贯通。故写此记,方便日后查阅。直至明了。 正文 在高并发或者大量的数据请求的场景中,我们会遇到很多问题,垃圾回收就是其中之一(garbage collection),为了减少优化GC,我们一般想到的方法就是能够让对象得以重用。这就需要一个对象池来存储待回收对象,等待下次重用,从而减少对象产生数量。我们可以把sync.Pool类型值看作是存放可被重复使用的值的容器。此类容器是自动伸缩的、高效的,同时也是并发安全的。为了描述方便,我们也会把sync.Pool类型的值称为临时对象池,而把存于其中的值称为对象值。这个类设计的目的是用来保存和复用临时对象,以减少内存分配,降低CG压力。 我们看下Go的源码: // Copyright 2013 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 sync import ( "internal/race" "runtime" "sync/atomic" "unsafe" ) // A