V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  icexin  ›  全部回复第 3 页 / 共 4 页
回复总数  67
1  2  3  4  
@scukmh defer 里面的 err 会把 return 的 err 给覆盖了,io.Copy 即使出错也发现不了。
2019-10-09 17:21:32 +08:00
回复了 dhairoot 创建的主题 程序员 怎么克服学习 Go 时的恶心感觉,语法太奇怪了
@janus77 go 的设计目标里面有快速编译,而没用的 import 会拖慢编译速度,代码越多影响就越大。详细看看官方的解释 https://golang.org/doc/faq#unused_variables_and_imports
2019-09-23 16:55:10 +08:00
回复了 zjsxwc 创建的主题 程序员 php7 怎么比 Java 还快?
golang 慢是因为它里面的 int 在 64 位环境下是 64 位的,把其他语言换成 int64 时间就很接近了
2019-09-14 19:33:30 +08:00
回复了 whoami9894 创建的主题 Go 编程语言 实在受不了 VScode 写 Go 的体验了, GoLand 真香
可能是项目的依赖没有下载完,先用 go mod download 预先下载 module cache 会好些。
2019-05-28 19:40:44 +08:00
回复了 gramyang 创建的主题 Go 编程语言 一个和 go 断言有关的神奇写法
断言到一个匿名接口
2019-05-28 11:53:51 +08:00
回复了 fengjianxinghun 创建的主题 Go 编程语言 go 编译成 c shared 库使用 luaffi load 会死锁
fork+多线程会有很多 bug
2019-05-13 11:58:51 +08:00
回复了 lueffy 创建的主题 Go 编程语言 A Tour of Go-Pointer receivers 一点疑问
为了写代码方便。否则第一个例子你得这么写(&v).Scale(10),go 默认帮你加上了取地址符,前提是变量本身是可以取地址的。
如果你写了一个签名为 func GetVertex() Vertex 的函数,GetVertex().Scale(10)就会失败,因为返回的临时变量不可取地址。
2019-05-13 11:54:13 +08:00
回复了 hahaDashen 创建的主题 Go 编程语言 有大佬来解答下关于 Golang flate/gzip/zlib 库的问题咩
看 php 文档 gzcompress 对应 zlib 压缩,我用 go 的 zlib 是可以解出来 php 压缩的数据
标准库涵盖了大部分场景,即使第三方库,看中了 github 上的哪个库,直接 import,自动分析依赖。带 gc,心智负担小。总之一句话,不折腾。
2019-01-21 19:48:02 +08:00
回复了 imherer 创建的主题 Go 编程语言 如下格式的返回码用 Go 应该如何实现
var (
SUCCESS = map[string]interface{}{"code":0, "msg": "操作成功"}
FAIL = map[string]interface{}{"code":100, "msg": "未知错误"}
)
或者定义一个包含 code 和 msg 字段的结构体,SUCCESS 和 FAIL 是对应的实例
2018-12-05 18:56:54 +08:00
回复了 rayhy 创建的主题 Go 编程语言 Golang 中 http.Get 的耗时比 curl 耗时大很多是什么原因?
可能是 dns cache 的问题,看下 net 下的文档 https://godoc.org/net#hdr-Name_Resolution
类 unix 系统默认是纯 go 的实现走 dns 协议 Lookup,windows 默认是走系统调用,可能有 cache 加速。
你在 wsl 下加上 export GODEBUG=netdns=cgo 强制走系统查询试试

The method for resolving domain names, whether indirectly with functions like Dial or directly with functions like LookupHost and LookupAddr, varies by operating system.

On Unix systems, the resolver has two options for resolving names. It can use a pure Go resolver that sends DNS requests directly to the servers listed in /etc/resolv.conf, or it can use a cgo-based resolver that calls C library routines such as getaddrinfo and getnameinfo.

By default the pure Go resolver is used, because a blocked DNS request consumes only a goroutine, while a blocked C call consumes an operating system thread. When cgo is available, the cgo-based resolver is used instead under a variety of conditions: on systems that do not let programs make direct DNS requests (OS X), when the LOCALDOMAIN environment variable is present (even if empty), when the RES_OPTIONS or HOSTALIASES environment variable is non-empty, when the ASR_CONFIG environment variable is non-empty (OpenBSD only), when /etc/resolv.conf or /etc/nsswitch.conf specify the use of features that the Go resolver does not implement, and when the name being looked up ends in .local or is an mDNS name.

The resolver decision can be overridden by setting the netdns value of the GODEBUG environment variable (see package runtime) to go or cgo, as in:

export GODEBUG=netdns=go # force pure Go resolver
export GODEBUG=netdns=cgo # force cgo resolver
The decision can also be forced while building the Go source tree by setting the netgo or netcgo build tag.

A numeric netdns setting, as in GODEBUG=netdns=1, causes the resolver to print debugging information about its decisions. To force a particular resolver while also printing debugging information, join the two settings by a plus sign, as in GODEBUG=netdns=go+1.

On Plan 9, the resolver always accesses /net/cs and /net/dns.

On Windows, the resolver always uses C library functions, such as GetAddrInfo and DnsQuery.
2018-12-05 17:51:10 +08:00
回复了 rayhy 创建的主题 Go 编程语言 Golang 中 http.Get 的耗时比 curl 耗时大很多是什么原因?
或者不想手写 httptrace 的话,试一下 github.com/davecheney/httpstat,这个是用 net/http 加 net/http/httptrace 做的工具,可以详细展示各个阶段的耗时。

➜ httpstat http://httpbin.org/get

Connected to 52.86.186.182:80

HTTP/1.1 200 OK
Server: gunicorn/19.9.0
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Length: 231
Content-Type: application/json
Date: Wed, 05 Dec 2018 09:49:00 GMT
Via: 1.1 vegur
Connection: keep-alive

Body discarded

DNS Lookup TCP Connection Server Processing Content Transfer
[ 612ms | 375ms | 237ms | 0ms ]
| | | |
namelookup:612ms | | |
connect:988ms | |
starttransfer:1225ms |
total:1225ms
2018-12-05 17:22:36 +08:00
回复了 rayhy 创建的主题 Go 编程语言 Golang 中 http.Get 的耗时比 curl 耗时大很多是什么原因?
用 httptrace 包看一下在哪里耗时了 https://godoc.org/net/http/httptrace#WithClientTrace
result[1].Interface().(error)
2018-10-26 21:35:24 +08:00
回复了 IsaacYoung 创建的主题 Go 编程语言 菜鸟问一个关于 goroutine 和闭包的问题
代码改成这样你就能明白了
``` go
package main

import (
"fmt"
"time"
)

type field struct {
name string
}

func print(p *field) {
fmt.Println(p.name)
}

func TestClosure() {

data := []*field{{"one"}, {"two"}, {"three"}}

for _, v := range data {
go print(v)
}

time.Sleep(3 * time.Second)
}

func TestClosure1() {

data := []field{{"one"}, {"two"}, {"three"}}

for _, v := range data {
go print(&v)
}

time.Sleep(3 * time.Second)
}

func TestClosure2() {

data := []*field{{"one"}, {"two"}, {"three"}}

for _, v := range data {
go func(){
print(v)
}()
}

time.Sleep(3 * time.Second)
}

func main(){
TestClosure()
fmt.Println("----")
TestClosure1()
fmt.Println("----")
TestClosure2()
}

```

`go print(v)`会在执行函数前把参数(v)以值拷贝的方式固定住
`go func(){print(v)}` 会在执行的时候再获取参数(v)的值
2018-10-11 11:49:24 +08:00
回复了 EchoUtopia 创建的主题 Go 编程语言 golang slice 一个易错点,今天我被卡了一个多小时
go 里面的 slice 是一个 descriptor 结构,包含 ptr, len, cap 三个字段,切片只是修改 len 和 cap,时间复杂度为 O(1)。
python 的 slice 是一次新建一个数组再拷贝的操作,时间复杂度为 O(n)。两者设计理念不一样。
2018-08-24 15:45:22 +08:00
回复了 Lighfer 创建的主题 问与答 Go 字节对齐的问题?
注释不是说了嘛,为了让 Stats 字段对齐。
看一下 sync/atomic 的文档 https://godoc.org/sync/atomic#pkg-note-bug
在 32 位平台下,原子操作会出问题,需要用户手动对齐。
2018-08-09 20:24:40 +08:00
回复了 kurtshiwz 创建的主题 Go 编程语言 golang 远程开发环境怎么配置
spacemacs,开箱即用
2018-06-20 20:15:52 +08:00
回复了 lauix 创建的主题 程序员 GO 如何实现 页面非阻塞?
看着是这样的,我一个开隐身,一个不开就没问题
2018-06-20 19:41:43 +08:00
回复了 lauix 创建的主题 程序员 GO 如何实现 页面非阻塞?
chrome 的锅。打开 console,看下请求时间主要在 stalled 上,由于两次请求是同一个资源,chrome 加了一把锁,导致同步请求。如果把 console 里面的 disable cache 勾上就没事了。
见这篇文章 http://fex.baidu.com/blog/2015/01/chrome-stalled-problem-resolving-process/
1  2  3  4  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4039 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 42ms · UTC 04:12 · PVG 12:12 · LAX 21:12 · JFK 00:12
Developed with CodeLauncher
♥ Do have faith in what you're doing.