golang

golang查询数据遇到空值问题

老子叫甜甜 提交于 2019-12-03 04:31:32
golang在查询数据库的时候遇到有空值的列,那么后面的字段提取就会失败,针对这种情况可以有2个办法解决: 1.限制数据库不为空或给个默认值,或是在select查询的时候对空值进行处理,使其查出来的数据不为空 2.采用sql.nullstring进行处理 下面的例子就是sql.sqlstring处理: package main import ( _ "github.com/go-sql-driver/mysql" "fmt" "database/sql" ) func nulldel(){ var err error connect := "dmladmin:dmladmin@tcp(192.168.1.113:3306)/db_admin" db, err := sql.Open("mysql", connect) if err != nil{ fmt.Println("connect mysql failed, address = " + connect, err) } else{ sqlContent := `select t.statedate, t.table_schema, t.table_name, t1.table_comment, t.table_rows, t.lm_table_rows, case when t.lm_table_rows > 0 then

golang strings.NewReader

荒凉一梦 提交于 2019-12-03 03:38:34
为什么说strings.Reader类型的值可以高效地读取字符串 与strings.Builder类型恰恰相反,strings.Reader类型是为了高效读取字符串而存在的。后者的高效主要体现在它对字符串的读取机制上,它封装了很多用于在string值上读取内容的最佳实践。 strings.Reader类型的值(以下简称Reader值)可以让我们很方便地读取一个字符串中的内容。在读取的过程中,Reader值会保存已读取的字节的计数(以下简称已读计数)。 已读计数也代表着下一次读取的起始索引位置。Reader值正是依靠这样一个计数,以及针对字符串值的切片表达式,从而实现快速读取。 此外,这个已读计数也是读取回退和位置设定时的重要依据。虽然它属于Reader值的内部结构,但我们还是可以通过该值的Len方法和Size把它计算出来的 Reader值拥有的大部分用于读取的方法都会及时地更新已读计数。比如,ReadByte方法会在读取成功后将这个计数的值加1。 又比如,ReadRune方法在读取成功之后,会把被读取的字符所占用的字节数作为计数的增量。 不过,ReadAt方法算是一个例外。它既不会依据已读计数进行读取,也不会在读取后更新它。正因为如此,这个方法可以自由地读取其所属的Reader值中的任何内容。 // 示例1。 reader1 := strings.NewReader(

golang subject dn from x509 cert

匿名 (未验证) 提交于 2019-12-03 03:10:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there any easy way to get the complete subject DN (or issuer DN) from an x509 certificate in go as a string? I was not able to find any methods like ".String()" in pkix.Name 回答1: Solution (thanks to a colleague): var oid = map[string]string{ "2.5.4.3": "CN", "2.5.4.4": "SN", "2.5.4.5": "serialNumber", "2.5.4.6": "C", "2.5.4.7": "L", "2.5.4.8": "ST", "2.5.4.9": "streetAddress", "2.5.4.10": "O", "2.5.4.11": "OU", "2.5.4.12": "title", "2.5.4.17": "postalCode", "2.5.4.42": "GN", "2.5.4.43": "initials", "2.5.4.44": "generationQualifier", "2.5

Dynamic loading in Golang?

匿名 (未验证) 提交于 2019-12-03 03:10:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've got a common project and small projects which act such as connectors in the common project. I want to create a common project such that when a new connector is developed I don’t have to modify code in the common project. Is it possible to dynamically load the structures in Go, only knowing the path (by putting this path in a file in common project and at runtime load that struct) of the struct and its folders? connector1 connector1.go /util /domain connectorN connectorN.go /domain commonProject main.go config.ini Structure config.ini

Golang ListenAndServeTLS returns data when not using https in the browser

匿名 (未验证) 提交于 2019-12-03 03:05:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: The following is my tls backend: package main import ( "fmt" "net/http" ) const ( PORT = ":8443" PRIV_KEY = "./private_key" PUBLIC_KEY = "./public_key" ) func rootHander(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Nobody should read this.") } func main() { http.HandleFunc("/", rootHander) err := http.ListenAndServeTLS(PORT, PUBLIC_KEY, PRIV_KEY, nil) if err != nil { fmt.Printf("main(): %s\n", err) } } The keys are generated using these two lines: openssl genrsa -out private_key 2048 openssl req -new -x509 -key private_key -out

Raw Socket Sniffing in Golang

匿名 (未验证) 提交于 2019-12-03 03:05:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a question concerning tcp packet sniffing with golang. We have written a small tool which captures all incoming TCP packets comming from a fibre Tap. The current implementation uses a libpcap wrapper under Linux. We need to port this tool to Windows. Of course, it is not possible at the moment. So my question is, is there a cross platform solution for sniffing packets? We only need TCP packets, IP headers , no Ethernet Data and not all features of libpcap. If there is no cross platform solution, two Code implementation would be ok,

Go golang, syntax error: unexpected ++, expecting :

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: func test(args ...string) { var msg map[string] interface{} i := 0 msg["product"] = args[i++] msg["key"] = args[i++] msg["signature"] = args[i++] msg["string_to_sign"] = args[i++] } go build utils.go after compile, I get the error message ./utils.go:28: syntax error: unexpected ++, expecting : ./utils.go:28: missing statement after label ./utils.go:29: syntax error: unexpected ++, expecting : ./utils.go:30: syntax error: unexpected ++, expecting : ./utils.go:31: syntax error: unexpected ++, expecting : ./utils.go:36: syntax error: unexpected

Can't get Golang to work in Ubuntu

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Ok, So I've downloaded Go 1.1 and put it into $HOME/Documents/go. Then, I've modified my .bashrc to be: export GOPATH=$HOME/Documents/go export GOROOT=$GOPATH export GOARCH=amd64 export GOOS=linux export GOBIN=$GOPATH/bin export PATH=$PATH:$GOBIN Than I've sourced the .bashrc , and tried: jan@janpc:~$ go version go version go1.1 linux/amd64 But I can't get it to compile or install any dependencies. Eg. I try to run my little test program: jan@janpc:~/Documents/go/src/github.com/jan/scrypt$ go run scrypt.go warning: GOPATH set to GOROOT (

print readable variables with golang

匿名 (未验证) 提交于 2019-12-03 02:52:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How to print a map, struct or whatever in a readable way? With PHP you can to this echo '<pre>'; print_r($var); echo '</pre>'; or header('content-type: text/plain'); print_r($var); 回答1: Use the Go fmt package. For example, package main import "fmt" func main() { variable := "var" fmt.Println(variable) fmt.Printf("%#v\n", variable) header := map[string]string{"content-type": "text/plain"} fmt.Println(header) fmt.Printf("%#v\n", header) } Output: var "var" map[content-type:text/plain] map[string]string{"content-type":"text/plain"} Package fmt

Nullable time.Time in golang

匿名 (未验证) 提交于 2019-12-03 02:45:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a struct that I intend to populate with a database record, one of the datetime columns is nullable: type Reminder struct { Id int CreatedAt time.Time RemindedAt *time.Time SenderId int ReceiverId int } Since pointers can be nil , I've made RemindedAt a pointer, but this will require the code to know the difference between the At variables. Is there a more elegant way to handle this? 回答1: You can use pq.NullTime . From lib/pq on github: type NullTime struct { Time time.Time Valid bool // Valid is true if Time is not NULL } // Scan