随笔分类 -  golang

golang学习
摘要:客户端client package main import ( "bufio" "fmt" "net" "os" "strings" ) //客户端 func main() { conn,err := net.Dial("tcp","127.0.0.1:20000") if err !=nil{ f 阅读全文
posted @ 2021-01-22 16:52 pebblecome 阅读(63) 评论(0) 推荐(0)
摘要:安装 go get github.com/go-sql-driver/mysql 代码示例 package main import ( "context" "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" "log" "strconv" 阅读全文
posted @ 2021-01-22 13:05 pebblecome 阅读(163) 评论(0) 推荐(0)
摘要:代码示例 package main import ( "fmt" "log" "net/http" "time" ) func setCookie(w http.ResponseWriter,r *http.Request) { expiration := time.Now().Add(time.H 阅读全文
posted @ 2021-01-22 12:58 pebblecome 阅读(119) 评论(0) 推荐(0)
摘要:编号1,2,3,4 的goroutine 每秒顺序打印 1,2,3,4 的编号 package main import ( "fmt" "time" ) //编号1,2,3,4 的goroutine 每秒顺序打印 1,2,3,4 的编号 type Token struct { } func work 阅读全文
posted @ 2021-01-22 12:55 pebblecome 阅读(105) 评论(0) 推荐(0)
摘要:代码示例 package main import ( "bufio" "fmt" "math/rand" "os" "strings" "time" ) //使用channel简单实现消息队列 //1 func main() { ch := make(chan string,1) rand.Seed 阅读全文
posted @ 2021-01-22 12:53 pebblecome 阅读(792) 评论(0) 推荐(0)
摘要:互斥锁 package main import ( "fmt" "sync" "time" ) //使用互斥锁计算时间 var( mu sync.Mutex x int64 wg sync.WaitGroup ) func write() { defer wg.Done() mu.Lock() x 阅读全文
posted @ 2021-01-22 12:51 pebblecome 阅读(94) 评论(0) 推荐(0)
摘要:代码示例 package main import "fmt" //实现map reduce func mapChan(in chan interface{},fn func(interface{}) interface{}) chan interface{}{ out := make(chan in 阅读全文
posted @ 2021-01-22 12:49 pebblecome 阅读(332) 评论(0) 推荐(0)
摘要:代码示例 package main import ( "fmt" "time" ) //使用channel 实现互斥锁 type Mutex struct { ch chan struct{} } //初始化锁 func NewMutex() *Mutex { mu := &Mutex{make(c 阅读全文
posted @ 2021-01-22 12:47 pebblecome 阅读(592) 评论(0) 推荐(0)
摘要:1,once 方式实现 package main import ( "sync" "sync/atomic" ) //单例模式 type Singleton struct {} var instance *Singleton var once sync.Once var mu sync.Mutex 阅读全文
posted @ 2021-01-22 12:45 pebblecome 阅读(272) 评论(0) 推荐(0)
摘要:文件读实现示例 func ReadFileByRead(filename string){ file,err := os.Open(filename) if err !=nil{ fmt.Println("open file failed err :",err) return } defer fil 阅读全文
posted @ 2021-01-22 12:43 pebblecome 阅读(129) 评论(0) 推荐(0)
摘要:安装 go get github.com/shirou/gopsutil 简单的代码示例 package main import ( "fmt" "github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/disk" host2 "git 阅读全文
posted @ 2021-01-22 12:40 pebblecome 阅读(725) 评论(0) 推荐(0)
摘要:下载: go get -u github.com/jinzhu/gorm 简单代码: package main import ( "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/mysql" "fmt" ) type Admin 阅读全文
posted @ 2021-01-22 12:37 pebblecome 阅读(85) 评论(0) 推荐(0)
摘要:GET请求示例 import ( "encoding/json" "fmt" "io/ioutil" "net/http" "net/url" ) type Result struct { Args string `json:"args"` Headers map[string]string `js 阅读全文
posted @ 2021-01-22 12:35 pebblecome 阅读(2553) 评论(0) 推荐(0)
摘要:代码示例 package main import ( "fmt" "os" "time" ) //time包练习 func main() { timeone := "" //当前时间 now := time.Now() time1 := now.Format("2006-01-02 15:04:05 阅读全文
posted @ 2021-01-22 12:33 pebblecome 阅读(55) 评论(0) 推荐(0)
摘要:下载安装 go get -u golang.org/x/sync/semaphore 简单的代码实现 package main import ( "context" "fmt" "golang.org/x/sync/semaphore" "log" "runtime" "time" ) /* 我们创 阅读全文
posted @ 2021-01-22 12:32 pebblecome 阅读(98) 评论(0) 推荐(0)
摘要:Kafka介绍 Kafka是Apache软件基金会开发的一个开源流处理平台,由Java和Scala编写;Kafka是一种高吞吐、分布式、基于订阅发布的消息系统。 Kafka名称解释 Producer:生产者 Consumer:消费者 Topic:消息主题,每一类的消息称之为一个主题 Broker:K 阅读全文
posted @ 2021-01-22 11:52 pebblecome 阅读(427) 评论(0) 推荐(0)
摘要:直接上代码,看怎么使用吧 package main import ( "fmt" "github.com/sirupsen/logrus" "os" ) var log = logrus.New() func initLog() error { file,err := os.OpenFile("lo 阅读全文
posted @ 2021-01-22 11:49 pebblecome 阅读(281) 评论(0) 推荐(0)
摘要:安装 go get github.com/spf13/viper Viper需要最少知道在哪里查找配置文件的配置。Viper支持JSON、TOML、YAML、HCL、envfile和Java properties格式的配置文件。Viper可以搜索多个路径,但目前单个Viper实例只支持单个配置文件。 阅读全文
posted @ 2021-01-22 11:46 pebblecome 阅读(94) 评论(0) 推荐(0)
摘要:直接代码吧,都是我自己写的测试代码 package main import "fmt" type NodeList struct { data int next *NodeList } func ShowNode(node *NodeList) { for node !=nil { //fmt.Pr 阅读全文
posted @ 2021-01-22 11:40 pebblecome 阅读(116) 评论(0) 推荐(1)
摘要:直接上代码 package main import ( "bufio" "container/list" "fmt" "os" "strings" ) //使用list实现一个队列 func main() { lt := list.New() consol := bufio.NewScanner(o 阅读全文
posted @ 2021-01-22 11:37 pebblecome 阅读(350) 评论(0) 推荐(0)