golang

golang vs dlang vs nodejs vs php 性能对比较量

本秂侑毒 提交于 2019-12-03 18:35:52
这是我本机开的一个单核1G内存的Hyper-V虚拟机,首先我们使用的语言和框架版本给大家看一下: root@kerisy:/home/zoujiaqing# go version go version go1.5.1 linux/amd64 root@kerisy:/home/zoujiaqing# ldc2 --version LDC - the LLVM D compiler (0.15.0): based on DMD v2.066.1 and LLVM 3.5.0 Default target: x86_64-pc-linux-gnu Host CPU: core-avx2 http://dlang.org - http://wiki.dlang.org/LDC Registered Targets: aarch64 - AArch64 (little endian) aarch64_be - AArch64 (big endian) arm - ARM arm64 - AArch64 (little endian) arm64_be - AArch64 (big endian) armeb - ARM (big endian) cpp - C++ backend hexagon - Hexagon mips - Mips mips64 - Mips64

Golang 实现华为云 DMS 签名

流过昼夜 提交于 2019-12-03 14:35:10
构造请求 首先构造请求,也就是要对哪个具体接口进行访问,需要提供什么必要的参数。在构造请求 (点击查看 中可以看到,对 DMS 服务来说必要的请求构成包括以下部分 请求URI,例如 https://dms.cn-north-1.myhuaweicloud.com/v1.0/{project_id}/queues/{quque_id} (不同区域的Region部分不同) 请求方法,如 "GET"、"POST" 请求消息头,必选的是 "Content-Type",规定了消息体的格式,默认取值 "application/json",即消息体 "Body" 以 Json 格式提交 请求消息体,有的接口需要,有的接口不需要 准备参数 以接口查看指定队列( 点击查看 )为例 请求URI: https://dms.cn-north-1.myhuaweicloud.com/v1.0/{project_id}/queues/{quque_id} 请求方法:"GET" 请求消息体:无 project_id:项目ID,去统一身份认证的项目中根据区域不同查找 queue_id:要访问的队列ID,去控制台点开该队列查看 AK/SK:秘钥对,在统一身份认证内的用户的安全设置功能中管理,是生成签名的必需参数 计算签名 以AK/SK签名认证算法详解( 点击查看 )为标准,编写算法或使用 SDK

几个golang 静态资源嵌入包

浪子不回头ぞ 提交于 2019-12-03 13:58:17
静态资源嵌入二进制文件中,可以方便我们的软件分发(只需要简单的二进制文件就可以了),目前大部分golang 的 web 应用都是使用类似的方法。 以下是收集到的一些常见方案 github.com/go-bindata/go-bindata go-bindata 的使用方法是先生成代码,然后使用提供的api引用 使用流程 生成资源代码 go-bindata data/ 通用引用 data, err := Asset("pub/style/foo.css") if err != nil { // Asset was not found. } ​ // use asset data http server 引用 go-bindata -fs -prefix "static/" static/ mux := http.NewServeMux() mux.Handle("/static", http.FileServer(AssetFile())) http.ListenAndServe(":8080", mux) github.com/elazarl/go-bindata-assetfs go-bindata-assetfs 是go-bindata的包装,也需要生成代码,但是使用更加简单,便捷 使用流程 生成代码 go-bindata-assetfs data/... 资源引用 http

golang(三)

▼魔方 西西 提交于 2019-12-03 13:41:00
map (声明、初始化和 make) 1. 概念 2.map 容量 3. 用切片作为 map 的值 概念 map 是引用类型,可以使用如下声明: var map1 map[keytype]valuetype var map1 map[string]int ( [keytype]``和 valuetype` 之间允许有空格,但是 gofmt 移除了空格) 在声明的时候不需要知道 map 的长度,map 是可以动态增长的。 未初始化的 map 的值是 nil。 key 可以是任意可以用 == 或者 != 操作符比较的类型,比如 string、int、float。所以数组、切片和结构 体不能作为 key,但是指针和接口类型可以。如果要用结构体作为 key 可以提供 Key() 和 Hash() 方 法,这样可以通过结构体的域计算出唯一的数字或者字符串的 key。 value 可以是任意类型的; 通过使用空接口类型,我们可以存储任意值,但是使用这种 类型作为值时需要先做一次类型断言。 map 传递给函数的代价很小:在 32 位机器上占 4 个字节,64 位机器上占 8 个字节,无论实际上存储了 多少数据。通过 key 在 map 中寻找值是很快的,比线性查找快得多,但是仍然比从数组和切片的索引中 直接读取要慢 100 倍;所以如果你很在乎性能的话还是建议用切片来解决问题。 map

golang字符串常用的系统函数

跟風遠走 提交于 2019-12-03 12:17:09
1.统计字符串的长度,按字节len(str)   str := "hello北京"   fmt.Println("str len=", len(str)) 2.字符串遍历,同时处理有中文的问题 r := []rune(str) str2 := "hello北京" r := []rune(str2) for i := 1; i < len(r); i++ { fmt.Printf("字符=%c\n", r[i]) } 3.字符串转整数: n, err := strconv.Atoi("12")    n, err := strconv.Atoi("5656") if err != nil{ fmt.Println("转换错误", err) }else { fmt.Println("转成的结果是", n) } 4.整数转字符串 str = strconv.Itoa(12345) str = strconv.Itoa(123456) fmt.Printf("str=%v, str=%T", str, str) 5.字符串转 []byte: var bytes = []byte("hello go") var bytes = []byte("hello go") fmt.Printf("bytes=%v\n", bytes) 6.[]byte 转字符串: str = string([

golang中遍历汇总

北城余情 提交于 2019-12-03 10:50:59
直接上例子: 例子1: package main import( "fmt" ) func main(){ a := map[string]string{ "alice":"11", "bob":"29", "zhangsan":"29", "wang":"35", } tmpRs := map[string][]string{} for k1, v1 := range a { tmpRs[v1] = append(tmpRs[v1], k1) } fmt.Printf("tmpRs.........=%v",tmpRs) } 根据value,把Value相同的放在一起,结果放在一个大map里. 执行结果如下: > tmpRs.........=map[11:[alice] 29:[bob zhangsan] 35:[wang]] 例子2: // json.go package main import ( "encoding/json" "fmt" "strconv" ) type FakeUserInfo struct { Push_Type string `json:"push_type"` // 渠道 Third_Token string `json:"third_token"` // token User_id string `json:"user_id"` // 用户ID

Golang file upload to s3 using OS Open

匿名 (未验证) 提交于 2019-12-03 10:24:21
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to upload an Image to my s3 account using Golang and the amazon s3 api . I can get the imagine uploaded if I hard code the direct path such as file, err := os.Open("/Users/JohnSmith/Documents/pictures/cars.jpg") defer file.Close() if err != nil { fmt.Printf("err opening file: %s", err) } if I hard code the file path like that then the picture will be uploaded to my s3 account . However that approach is not good as I can't obviously hard code the direct image path to every image that I want to upload . My question is how can I

[golang] 创建同一个变量

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 10:22:12
在阅读别人的工程代码时,发现有一个函数内调用的方法都是返回一个不同类型的值+error。 显然,为了避免错误,除了获取目的返回值,还应该获取返回的error,判断它有没有出错。 而工程代码中的写法是: aaa, err := function(……)if err != nil {  return ……} bbb, err := function(……)if err != nil {  return ……} 是的,没错,工程代码中使用了“:=”创建了同一个变量err。 不是说“:=”左边必须是一个新的变量吗?于是编写试验代码: package main import ( "fmt" ) func main() { val := 123 val := 456 fmt.Println(val) } 果不其然,报“no new variables on left side of :=”错。 于是换一种方法: package main import ( "fmt" ) func main() { bytes, kk := 789, 123 result, kk := 112, 456 fmt.Println(bytes, result) fmt.Println(kk) } 发现竟然编译通过。 原理尚不清楚,待日后再深究。 来源: https://www.cnblogs.com

golang: core net/http package import errors

匿名 (未验证) 提交于 2019-12-03 09:13:36
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've cloned go source code using git clone https://go.googlesource.com/go into my ~/godev/ directory (outside of GOPATH as the docs advise). My $GOPATH is ~/gocode I installed go 1.8.1 using the official osx installer. If I cd into ~/godev/go/src/net/http and run go test , I get these errors: h2_bundle.go:46:2: cannot find package "golang_org/x/net/http2/hpack" in any of: /usr/local/go/src/golang_org/x/net/http2/hpack (from $GOROOT) ~/gocode/src/golang_org/x/net/http2/hpack (from $GOPATH) h2_bundle.go:47:2: cannot find package "golang_org/x

golang: core net/http package import errors

匿名 (未验证) 提交于 2019-12-03 09:13:36
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've cloned go source code using git clone https://go.googlesource.com/go into my ~/godev/ directory (outside of GOPATH as the docs advise). My $GOPATH is ~/gocode I installed go 1.8.1 using the official osx installer. If I cd into ~/godev/go/src/net/http and run go test , I get these errors: h2_bundle.go:46:2: cannot find package "golang_org/x/net/http2/hpack" in any of: /usr/local/go/src/golang_org/x/net/http2/hpack (from $GOROOT) ~/gocode/src/golang_org/x/net/http2/hpack (from $GOPATH) h2_bundle.go:47:2: cannot find package "golang_org/x