golang

Golang http client handshake failure

匿名 (未验证) 提交于 2019-12-03 08:57:35
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Try get webpage: tr := & http . Transport { TLSHandshakeTimeout : 30 * time . Second , DisableKeepAlives : true , } client := & http . Client { Transport : tr } req , err := http . NewRequest ( "GET" , "https://www.fl.ru/" , nil ) if err != nil { log . Fatalf ( "%s\n" , err ); } resp , err := client . Do ( req ); if err != nil { log . Fatalf ( "%s\n" , err ); } defer resp . Body . Close () Get https://www.fl.ru/ : remote error: handshake failure. If I try to get another HTTPS page - all is OK. 回答1: That server only supports a few,

Go - Golang openpg - Create Key pair and create signature

匿名 (未验证) 提交于 2019-12-03 08:52:47
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I'm currently working on openpgp in combination with golang. I use the following code to generate a new keypair and create a self-signature on the resulting public key: package main import ( "bytes" "crypto" "time" "golang.org/x/crypto/openpgp" "golang.org/x/crypto/openpgp/armor" "golang.org/x/crypto/openpgp/packet" "fmt" ) //Create ASscii Armor from openpgp.Entity func PubEntToAsciiArmor ( pubEnt * openpgp . Entity ) ( asciiEntity string ) { gotWriter := bytes . NewBuffer ( nil ) wr , errEncode := armor . Encode ( gotWriter ,

Multiple-types decoder in golang

匿名 (未验证) 提交于 2019-12-03 08:50:26
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an XML document. Some fields have custom format. Example: <document> <title>hello world</title> <lines> line 1 line 2 line 3 </lines> </document> I want to import it into structure like: type Document struct { Title string `xml:"title"` Lines []string `xml:"lines"` } Is there some way how to implement custom decoder, which will split lines string into array of lines ( ["line 1", "line 2", "line 3"] )? Its possible to make Lines field a string type and make split after xml import, but it doesn't seems to be very elegant solution. Is

Golang interface{} type misunderstanding

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I got a bug in Go when using an interface{} as function parameter type, when given a non-pointer type, and using json.Unmarshal with it. Because a piece of code is worth a thousand words, here is an example: package main import ( "encoding/json" "fmt" ) func test ( i interface {}) { j := [] byte ( `{ "foo": "bar" }` ) fmt . Printf ( "%T\n" , i ) fmt . Printf ( "%T\n" , & i ) json . Unmarshal ( j , & i ) fmt . Printf ( "%T\n" , i ) } type Test struct { Foo string } func main () { test ( Test {}) } Which outputs: main . Test *

golang multiple case in type switch

匿名 (未验证) 提交于 2019-12-03 08:46:08
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: when I run the code snippet bellow, it raise a error a.test undefined (type interface {} is interface with no methods) It seem the type switch does not take effect. package main import ( "fmt" ) type A struct { a int } func(this *A) test(){ fmt.Println(this) } type B struct { A } func main() { var foo interface{} foo = A{} switch a := foo.(type){ case B, A: a.test() } } If I change it to switch a := foo.(type){ case A: a.test() } it's ok now. 回答1: This is normal behaviour that is defined by the spec (emphasis mine): The TypeSwitchGuard may

Atom Editor Golang - Go To Declaration not working

匿名 (未验证) 提交于 2019-12-03 08:41:19
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've done a fresh install of atom and installed go-plus package. The one feature I can't get working is the "Go To Declaration" which is why I'm still stuck in sublime land... Does anyone know if this works with golang? I't appears that I need to have ctags for my project? Whats the best way to get this going for atom? I've tried installing other packages that geverate the ctags but I don't think that helped. Has anyone got this working? What did you do? 回答1: Fresh install of Atom 1.3.3 with go-plus 3.5.2 and it worked. By default, the

GoLang: Nested properties for structs with unknown property names?

匿名 (未验证) 提交于 2019-12-03 08:36:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I'm using json to get some values into a struct from an external source. Using UnMarshal to put the values in a struct. I have a struct like this that UnMarshal puts values into: type Frame struct { Type string Value map [ string ] interface {} } var data Frame After the UnMarshal, I can access type by: data.Type but if I try doing something like: if data . Type == 'image' { fmt . Println ( fmt . Sprintf ( "%s" , data . Value . Imagedata )) } The compiler complains about no such value data.Value.Imagedata So my question is, how do

How to append text to a file in golang? [duplicate]

匿名 (未验证) 提交于 2019-12-03 08:36:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: This question already has an answer here: Append to a file in Go 5 answers I find os.Open() return a O_RDONLY file and os.Create() return a O_RDWR but can't find a method return a APPEND file pointer. Any help ? 回答1: The OpenFile takes a flags argument that you can use: os . OpenFile ( "foo.txt" , os . O_RDWR | os . O_APPEND , 0660 ); Used with O_CREATE , OpenFile can also serve the same purpose as os.Create() 转载请标明出处: How to append text to a file in golang? [duplicate] 文章来源: How to append text to a file in golang? [duplicate]

Reflection error on GoLang - Too few arguments

匿名 (未验证) 提交于 2019-12-03 07:36:14
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have this controller: package web import ( "net/http" ) func init() { } func (controller *Controller) Index(r *http.Request) (string, int) { return "Testing", http.StatusOK } With this handler: type Application struct { } func (application *Application) Route(controller interface{}, route string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var ptr reflect.Value var value reflect.Value var finalMethod reflect.Value value = reflect.ValueOf(controller) // if we start with a pointer, we need to get value pointed to

关于golang.org/x包问题

风格不统一 提交于 2019-12-03 05:02:56
由于谷歌被墙,跟谷歌相关的模块无法通过go get来下载 解决方法: git clone https://github.com/golang/net.git $GOPATH/src/github.com/golang/net git clone https://github.com/golang/sys.git $GOPATH/src/github.com/golang/sys git clone https://github.com/golang/tools.git $GOPATH/src/github.com/golang/tools ln -s $GOPATH/src/github.com/golang $GOPATH/src/golang.org/x    如果是Windows下,最后一条可以替换为mklink命令,或者直接拷贝文件夹修改一下名称即可。 go的官方包都在这个目录下: https://github.com/golang 原文地址: https://studygolang.com/articles/7087 来源: https://www.cnblogs.com/saryli/p/11779112.html