Golang

golang chan的用法

实验目的 chan相关前置知识点,请参考本文底部链接。墙裂推荐阅读。 假设系统内除了主线程之外,有四个线程connector,sender,receiver,heartbeat connector,负责建立网络连接,在连接断开的时候进行重连 sender,负责发送数据报 receiver,负责接收数据报 heartbeat,负责监控心跳 如何启动connector connector需要等待ConnectorNotifier,值为1连接,值为2重连 建立连接之后,发送三次ConnectorConnected,通知sender, sender,receiver可以开工 var ConnectorConnected chan int = make(chan int, 3) // 已连接通知 func connector(){ ..... // 成功之后发送三次通告 ConnectorConnectedNotifier <- 1 ConnectorConnectedNotifier <- 1 ConnectorConnectedNotifier <- 1 } 如何启动sender, receiver,以及heartbeat 等待ConnectorConnectedNotifier,收到...

go语言数组内存是怎么扩容的

话不多说,上测试代码和结果 package main import ( "fmt" ) func main() { var orderIds []int fmt.Println(len(orderIds), cap(orderIds)) fmt.Println("已使用", "\t\t", "容量", "\t\t", "增量") var lastCap = cap(orderIds) for i := 0; i < 100000; i++ { orderIds = append(orderIds, 1) if cap(orderIds) != lastCap{ fmt.Println(len(orderIds), "\t\t", cap(orderIds), "\t\t", cap(orderIds)-lastCap) lastCap = cap(orderIds) } } } 输出 0 0 已使用 容量 增量 1 2 2 3 4 2 5 8 4 9 16 8 ...

golang数组用法

package main import ( "fmt" ) var ( duration int = 2 ) func main() { fmt.Println("test case 1:") strs := make([]string, 10) strs = append(strs, "1") strs = append(strs, "2") strs = append(strs, "3") for _, val := range strs{ fmt.Println(val) } fmt.Println("test case 2:") var strs2 []string strs2 = append(strs2, "1") strs2 = append(strs2, "2") strs2 = append(strs2, "3") for _, val2 := range strs2{ fmt.Println(val2) } } 输出 test case 1: 1 2 3 test case 2: 1 2 3

golang interface用法

golang interface测试 很不爽这种一眼看过去看不出一个struct实现了哪个interface的奇怪设计。但路依然是要走的,真香。。 测试代码1 package main import "fmt" type Task interface{ execute() } type CrontabTask struct{ } func (task CrontabTask) execute() { fmt.Println("crontab task!") } type QueueTask struct{ } func (task QueueTask) execute() { fmt.Println("queue task!") } type LoopTask struct{ } func (task LoopTask) execute() { fmt.Println("loop task!") } type WrongTask struct{ } func (task WrongTask) execute1() { fmt.Println("loop task!") } func main(){ var task Task task = new (CrontabTask) task.execut...

支持多种登录方式的用户表设计

支持多种登录方式的用户表设计 最原始的用户登录,user表 user_id username password 用户数据 1 xxxx 123456 用户数据 2 yyyy 123456 用户数据 select * from user where user = :user and password = :password 搞定了 如果要加入微信登录如何破 加三个字段? user_id username password wechat_id wechat_access_token wechat_expires 1 xxxx 123456 sasdada qwerty 00000000 再添加qq登录呢?再加三个字段,显然是要累死人的 靠谱的做法 用户信息和登录分开,登录分为本地登录和第三方登录 user表 user_id 用户数据 1 用户数据 2 用户数据 本地登录表 user_id username password 1 xxxx 123456 2 yyyy 123456 第三方OAuth登录表 id user_id oauth_name oauth_id oauth_access_token oauth_expires 1 A1 weibo W...

gomod使用方法

全在这里了 https://zhuanlan.zhihu.com/p/60703832

根据json生成golang struct

根据json生成golang struct https://mholt.github.io/json-to-go/ 输入 {"age":1, "firstName":"LeTian", "lastName":"Chu", "birthday":"2000-01-30"} 输出 type AutoGenerated struct { Age int `json:"age"` FirstName string `json:"firstName"` LastName string `json:"lastName"` Birthday string `json:"birthday"` }

windows安装gomicro

问题 安装gomicro发现各种莫名其妙的错误。 重点注意 翻墙,不知道某些孙子处于什么心态把golang/x墙了,祝尔等阖家欢乐 升级golang 1.13

golang实现类

介绍 类,熟悉java c++ php python等语言中任意一门的人都不会陌生。我假设不行看到这篇文章的人,和我一样有其他高级语言经验。所以这里不废话什么是class。 golang实现 golang并没有提供class关键字来定义类。但是他有struct,并且我们可以给struct添加方法。这样子我们也可以达到实现类功能的目的。 我们来实现一个带有加(add)减(minus)乘(multiple)除(divide)四种操作的Calculator类吧。 首先我们定义一个Calculator接口 package main import "fmt" // 定义计算器的操作 type Calculator interface { add(x int, y int) int minus(x int, y int) int multiply(x int, y int) int divide(x int, y int) float32 } // 定义TestCalculator"类" type TestCalculator struct{ } func (consoleCalculator TestCalculator) add(x int, y int) int { return x + y } func (consoleCalcul...

Golang实现接口编程

介绍 接口是面向对象编程一种重要技术手段。一个接口中包含一组标准操作的签名,任意class只要实现了这个接口的方法,就可以被当做这个接口的子类型来调用。因为这个特性,我们就可以实现功能组件的即插即用,随意替换。 举个cache的例子,这里用java来写: public class Main { public static void main(String[] args){ Cache cache; // 输出:Redis缓存内容 cache = new RedisCache(); System.out.println(cache.get("key")); // 输出:File缓存内容 cache = new FileCache(); System.out.println(cache.get("key")); } } // 缓存接口 interface Cache { Object get(String key); void set(String key, Object value); void delete(String key); } // 基于redis的缓存 class RedisCache implem...
执行时间: 1710849084566.5 毫秒