golang

golang中函数类型

非 Y 不嫁゛ 提交于 2019-11-29 14:33:38
今天看Martini文档,其功能列表提到完全兼容 http.HandlerFunc 接口,就去查阅了 Go: net/http 的文档,看到type HandlerFunc这部分,顿时蒙圈了。由于之前学习的时候没有关注过function types的知识点,就Google了一些文章,才算是有了个大概的了解。 从golang的官方文档得知 function types 的解释是这样的。 A function type denotes the set of all functions with the same parameter and result types. 先找个例子来看一下: package main import "fmt" // Greeting function types type Greeting func(name string) string func say(g Greeting, n string) { fmt.Println(g(n)) } func english(name string) string { return "Hello, " + name } func main() { say(english, "World") } 输出 Hello, World say()函数要求传入一个Greeting类型

2 golang中变量的定义

社会主义新天地 提交于 2019-11-29 11:35:55
个人理解的是,go里面的变量和const修饰的常量其实是不同的概念,做了一个彻底的区分。 对变量的定义:引入了关键字var对变量进行声明 var a int var b string var c float64 var e [4] int // 数组 var f [] int //数组切片 var f * int //正确 var v1 int = 5 //正确 var v2 = 5 //正确,编译器自动推导出V2类型 v3 := 5 //正确,编译器自动推导出V3的类型 多重赋值 Go语言提供了大多数的语言所不支持的多重赋值,使得变量的的变换变得十分简单,多重赋值的一个例子,就是不要像c语言一样使用中间的变量 i := 2 j := 3 i , j = i , i //交换i与j的值 变量的类型 获取变量的大小 //使用unsafe package main import ( "fmt" "unsafe" ) func main(){ var a int64 = 10000 fmt.Println("length of a : " , unsafe.Sizeof(a)) } 布尔类型 Go语言提供内置的布尔值true和false,Go语言支持标准的逻辑比较和比较操作,但是操作结果都是布尔值。在go里面是不允许将其他的类型转换为bool类型的,比如int类型

弄懂goroutine调度原理

随声附和 提交于 2019-11-29 10:19:01
goroutine简介 golang语言作者Rob Pike说, “Goroutine是一个与其他goroutines 并发运行在同一地址空间的Go函数或方法。一个运行的程序由一个或更多个goroutine组成。它与线程、协程、进程等不同。它是一个goroutine“ 。 goroutine通过通道来通信,而协程通过让出和恢复操作来通信; goroutine 通过Golang 的调度器进行调度,而协程通过程序本身调度; 简单的说就是Golang自己实现了协程并叫做goruntine(本文称Go协程),且比协程更强大。 goroutine调度原理 上面说到Go协程是通过Golang的调度器进行调度的,其中调度器的线程模型为两级线程模型。 有关两级线程模型的介绍,可以看 这篇文章 我们来看下Golang实现的两级线程模型是怎样的。首先要知道这三个字母代表的含义 M :代表内核级的线程 P :全程Processor,代表运行Go协程所需要的资源(上下文环境) G :代表Go协程 我们先看下为实现调度Golang定义了这些数据结构存M,P,G 名称 作用范围 描述 全局M列表 Go的运行时 存放所有M的单向链表 全局P列表 Go的运行时 存放所有P的数组 全局G列表 Go的运行时 存放所有G的切片 调度器的空闲M列表 调度器 存放空闲M的单向链表 调度器的空闲P列表 调度器

Golang读取并修改非主流配置文件

ぃ、小莉子 提交于 2019-11-29 10:16:51
今天工作中碰到的问题,要求修改此配置文件,没看出来是什么格式,用了下面的思路: mysql { # If any of the files below are set, TLS encryption is enabled tls { ca_file = "/etc/ssl/certs/my_ca.crt" ca_path = "/etc/ssl/certs/" certificate_file = "/etc/ssl/certs/private/client.crt" private_key_file = "/etc/ssl/certs/private/client.key" cipher = "DHE-RSA-AES256-SHA:AES128-SHA" tls_required = yes tls_check_cert = no tls_check_cert_cn = no } # If yes, (or auto and libmysqlclient reports warnings are # available), will retrieve and log additional warnings from # the server if an error has occured. Defaults to 'auto' warnings = auto }

golang gorm 错误处理

本小妞迷上赌 提交于 2019-11-29 10:11:39
func CreateAnimals(db *gorm.DB) err { tx := db.Begin() if err := tx.Create(&Animal{Name: "Giraffe"}).Error; err != nil { tx.Rollback() return err } if err := tx.Create(&Animal{Name: "Lion"}).Error; err != nil { tx.Rollback() return err } tx.Commit() return nil } 用 db.Begin() 声明开启事务,结束的时候调用 tx.Commit() ,异常的时候调用 tx.Rollback() 来源: https://www.cnblogs.com/wangjq19920210/p/11512893.html

golang ldfalgs的小技巧

会有一股神秘感。 提交于 2019-11-29 10:03:20
问题 如果想要在go build生成的可执行文件中注入编译时间,git hash等信息。可以在编译的时候使用-ldflags -X参数来注入变量 -ldfflags -X 可以在go install 、go build、go run 、go test中使用 go build -ldflags "-X ' packageName.varName=cmd ' " 样例 package main import "fmt" var ( time = "not set" git = "not set" ) func main() { fmt.Println("build time:", time) fmt.Println("git log:", git) } 想要在build的时候改变time和git的值,可以这么做: windows: go build -ldflags "-X 'main.buildTime=$(date)' -X 'main.gitHash=$(git log --pretty=format:"%h" -1)'" main.go linux: go build -ldflags "-X 'main.time=$(date -u --rfc-3339=seconds)' -X 'main.git=$(git log --pretty=format:"%h" -1)'"

golang学习网站

橙三吉。 提交于 2019-11-29 08:20:21
本文链接:https://blog.csdn.net/qq_41618084/article/details/82827633 Go轻松学: https://www.golang123.com/book/16?chapterID=292 Go示例学: https://www.golang123.com/book/17?chapterID=301 Go Web编程: https://www.golang123.com/book/9?chapterID=186 更多学习电子图书请到这里来: https://www.golang123.com/book 来源: https://blog.csdn.net/boshuzhang/article/details/100669447

golang 查看代码调用关系图

只愿长相守 提交于 2019-11-29 08:01:48
[TOC] go-callvis 是github上一个开源项目,可以用来查看golang代码调用关系。 安装 安装graphviz $ brew install graphviz 安装go-callvis go get -u github. com /TrueFurby/ go -callvis cd $GOPATH/src/github. com /TrueFurby/ go -callvis && make 用法 $ go-callvis [flags] package 示例 以orchestrator项目为例,其代码已经下载到本地。 $ go-callvis github.com/github/orchestrator/go/cmd/orchestrator 如果没有focus标识,默认是main 例如,查看 github.com/github/orchestrator/go/http 这个package下面的调用关系: $ go-callvis -focus github.com/github/orchestrator/go/http github.com/github/orchestrator/go/cmd/orchestrator 浏览器跳出页面 http://localhost:7878,可以看到代码调用关系图。 来源: https://blog.csdn.net

golang 使用 consul 做服务发现

狂风中的少年 提交于 2019-11-29 07:23:09
当我们服务越来越多,如果服务配置了弹性伸缩,或者当服务不可用时,我们需要随时动态掌握可以使用的服务数量,并向可提供响应的服务发送请求。这时我们需要服务发现功能,当新增服务时,服务可以自动向consul注册,客户端直接向consul发送请求,获取可用服务的地址和端口;当服务不可用时,动态的更新consul,删除该服务在consul中的列表 docker安装consul docker run --name consul1 -d -p 8500:8500 -p 8300:8300 -p 8301:8301 -p 8302:8302 -p 8600:8600 consul:latest agent -server -bootstrap-expect 2 -ui -bind=0.0.0.0 -client=0.0.0.0 8500 http 端口,用于 http 接口和 web ui 8300 server rpc 端口,同一数据中心 consul server 之间通过该端口通信 8301 serf lan 端口,同一数据中心 consul client 通过该端口通信 8302 serf wan 端口,不同数据中心 consul server 通过该端口通信 8600 dns 端口,用于服务发现 -bbostrap-expect 2: 集群至少两台服务器,才能选举集群leader -ui

golang depth read map

≯℡__Kan透↙ 提交于 2019-11-29 06:52:29
Foreword: I optimized and improved the below solution, and released it as a library here: github.com/icza/dyno . The cleanest way would be to create predefined types (structures struct ) that model your JSON, and unmarshal to a value of that type, and you can simply refer to elements using Selectors (for struct types) and Index expressions (for maps and slices). However if your input is not of a predefined structure, I suggest you the following 2 helper functions: get() and set() . The first one accesses (returns) an arbitrary element specified by an arbitrary path (list of string map keys and