golang

golang中的pflag示例

99封情书 提交于 2019-12-03 02:17:53
现在晚上在家啃kubeadm的源码, 在啃源码前,pflag,viper,cobra这三件套好像是必须的, 那就先弄懂一下这三个套件的套路吧。 第一个,pflag. https://www.cnblogs.com/sparkdev/p/10833186.html https://github.com/spf13/pflag pflag is a drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags. pflag is compatible with the GNU extensions to the POSIX recommendations for command-line options . For a more precise description, see the "Command-line flag syntax" section below. pflag 包与 flag 包的工作原理甚至是代码实现都是类似的,下面是 pflag 相对 flag 的一些优势: 支持更加精细的参数类型:例如,flag 只支持 uint 和 uint64,而 pflag 额外支持 uint8、uint16、int32 等类型。 支持更多参数类型:ip、ip mask、ip net

Instance new Type (Golang)

匿名 (未验证) 提交于 2019-12-03 02:14:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Can anyone tell me how to create a new instance of Type from a string? Reflect? There are examples but they are for the older (pre Go 1 versions) of the language [:(] 回答1: So, if I understand your question correctly, you are asking about how you can create an object when you just have the name of the type as string. So, for example, you might have a string "MyStruct" and you want to create an object of this type. Unfortunately, that's not easily possible because Go is a statically typed language and the linker will eliminate dead code (or

Webserver for Go (golang) webservices: using NGINX or not? [closed]

匿名 (未验证) 提交于 2019-12-03 02:12:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I am writing some webservices returning JSON data, which have lots of users. Would you recommend to use NGINX as a webserver or it is good enough to use the standard http server of Go? 回答1: It depends. Out of the box, putting nginx in front as a reverse proxy is going to give you: Access logs Error logs Easy SSL termination SPDY support gzip support Easy ways to set HTTP headers for certain routes in a couple of lines Very fast static asset serving (if you're serving off S3/etc. though, this isn't that relevant) The Go HTTP server

Webserver for Go (golang) webservices: using NGINX or not? [closed]

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I am writing some webservices returning JSON data, which have lots of users. Would you recommend to use NGINX as a webserver or it is good enough to use the standard http server of Go? 回答1: It depends. Out of the box, putting nginx in front as a reverse proxy is going to give you: Access logs Error logs Easy SSL termination SPDY support gzip support Easy ways to set HTTP headers for certain routes in a couple of lines Very fast static asset serving (if you're serving off S3/etc. though, this isn't that relevant) The Go HTTP server

Invoke golang struct function gives “cannot refer to unexported field or method”

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a golang structure something like this: type MyStruct struct { Id string } and function: func (m *MyStruct) id() { // doing something with id here } Also I have another structure like this: type MyStruct2 struct { m *MyStruct } Now I have a function: func foo(str *MyStruct2) { str.m.id() } But I'm getting error in compile time: str.m.id undefined (cannot refer to unexported field or method mypackage.(*MyStruct)."".id How can I call this function correctly? Thank you 回答1: From http://golang.org/ref/spec#Exported_identifiers : An

golang语言并发与并行——goroutine和channel的详细理解(一)

限于喜欢 提交于 2019-12-03 02:10:07
如果不是我对真正并行的线程的追求,就不会认识到 Go 有多么的迷人。 Go语言从语言层面上就支持了并发,这与其他语言大不一样,不像以前我们要用Thread库 来新建线程,还要用线程安全的队列库来共享数据。 以下是我入门的学习笔记。 Go语言的goroutines、信道和死锁 goroutine Go语言中有个概念叫做goroutine, 这类似我们熟知的线程,但是更轻。 以下的程序,我们串行地去执行两次 loop 函数: func loop () { for i := 0 ; i < 10 ; i ++ { fmt . Printf ( "%d " , i ) } } func main () { loop () loop () } 毫无疑问,输出会是这样的: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 下面我们把一个loop放在一个goroutine里跑,我们可以使用关键字 go 来定义并启动一个goroutine: func main () { go loop () // 启动一个goroutine loop () } 这次的输出变成了: 0 1 2 3 4 5 6 7 8 9 可是为什么只输出了一趟呢?明明我们主线跑了一趟,也开了一个goroutine来跑一趟啊。 原来,在goroutine还没来得及跑loop的时候,主函数已经退出了。

Golang之消息机制channel

大城市里の小女人 提交于 2019-12-03 02:07:08
1. 背景: 1. 对于以下这段代码: 按照想法应该输出 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 但是,输出结果是: 0 1 2 3 4 5 6 7 8 9 2. 原因: 在goroutine还未来得及跑loop函数时,主函数main已经退出。 解决主函数退出太快最直接的方法是让主函数睡眠一段时间: 这次输出结果确实是两趟。 可是等待的办法并不好,因为并不知goroutine要执行多长时间,只能尽可能长的设置主函数的sleep,这样会让主函数执行时间远大于实际执行时间。 3. 需要找一个方法,让goroutine快执行完时通知主函数,即阻塞住主routine,类似Java中的A.join()函数(调用方必须等待A执行完毕返回后再返回),那么涉及多个goroutine间通信,go中的channel就起了这样的作用。 2. channel 1.定义: channel是Golang语言在语言层级提供的goroutine间的通信方式,可以使用channel在多个goroutine之间传递消息。 2.使用: channel是类型相关的,一个channel只能传递一种类型的值,传递类型需要在声明channel时指定。 3.声明: 1. var chanName chan ElementType 举例:声明一个传递int类型的channel:var ch

golang之Channel

时光怂恿深爱的人放手 提交于 2019-12-03 02:06:41
Channel是Go中的一个核心类型,可以将其看成一个管道,通过它并发单元就可以发送或者接收数据进行通信(communication)。 Do not communicate by sharing memory; instead, share memory by communicating. channel基础知识 创建channel 使用内建函数make创建channel: unBufferChan := make ( chan int ) // 1 无缓冲的channel bufferChan := make ( chan int , N) // 2 带缓冲的channel 无缓冲: 发送和接收动作是同时发生的。如果goroutine读取channel(<-channel),则发送者(channel<-)会一直阻塞。 缓冲 channel 类似一个有容量的队列。当队列满的时候发送者会阻塞;当队列空的时候接收者会阻塞。 写出以下代码打印的结果: func main() { var x chan int go func () { x <- 1 }() <-x } 结果分析:往一个nil channel中发送数据会一直被阻塞,从一个nil channel中接收数据会一直被block,所以才会产生如下死锁的现象。 fatal error: all goroutines are

Go/GoLang check IP address in range

匿名 (未验证) 提交于 2019-12-03 02:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In Go/GoLang, what is the fastest way to check if an IP address is in a specific range? For example, given range 216.14.49.184 to 216.14.49.191 , how would I check if a given input IP address is in that range? 回答1: IP addresses are represented as bigendian []byte slices in go (the IP type) so will compare correctly using bytes.Compare . Eg ( play ) package main import ( "bytes" "fmt" "net" ) var ( ip1 = net.ParseIP("216.14.49.184") ip2 = net.ParseIP("216.14.49.191") ) func check(ip string) bool { trial := net.ParseIP(ip) if trial.To4() ==

Golang http request results in EOF errors when making multiple requests successively

匿名 (未验证) 提交于 2019-12-03 02:05:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I am trying to debug a very unusual error I am receiving for a simple REST library I wrote . I am using the standard net/http package to make Get, Post, Put, Delete requests but my tests occasionally fail when I make multiple requests successively. My test looks like this: func TestGetObject ( t * testing . T ) { firebaseRoot := New ( firebase_url ) body , err := firebaseRoot . Get ( "1" ) if err != nil { t . Errorf ( "Error: %s" , err ) } t . Logf ( "%q" , body ) } func TestPushObject ( t * testing . T ) { firebaseRoot := New (