Go语言中文网 为您找到相关结果 8997

What are best practices on errors with field data, passing errors up, wrapping errors, and checking error values?

<p>I understand that <code>error</code> is an interface, and that in theory there are lots of choices for what to satisfy that interface. However, in practice, what tends to be chosen?</p> <p>When is <code>errors.New</code> or <code>fmt.Errorf</code> preferred? Should those be the default, in t...阅读全文

How do I get go/ast.Package.Scope.Objects to be set?

<p>To get the ast.Package, I currently use go/build.Default.Import to get the go/build.Package, then use go/parser.ParseDir with a filter function that only allows the build.Package.GoFiles files. That all seems to work, and everything seems to get parsed, but the Objects I mentioned above is nil.</p> <p>(I need the package-level...阅读全文

关于收集,标准化和集中化处理Golang日志的一些建议

依赖分布式系统的公司组织和团队经常使用Go语言编写其应用程序,以利用Go语言诸如通道和goroutine之类的并发功能。如果你负责研发或运维Go应用程序,则考虑周全的日志记录策略可以帮助你了解用户行为,定位错误并监控应用程序的性能。 这篇文章将展开聊一些用于管理Go日志的工具和技术。我们将首先考虑要使用哪种日志记录包来满足各种记录要求。然后会介绍一些使日志更易于搜索和可靠,减少日志资源占用以及使日志消息标准化的技术。 日志包的选择 Go标准库的日志库非常简单,仅仅提供了print,panic和fatal三个函数对于更精细的日志级别、日志文件分割以及日志分发等方面并没有提供支持. 所以催生了很多第三方的日志库,流行的日志框架包括logrus、zap、glog等。我们先来大致看下这些日志库的特点...阅读全文

Best way to modify a json file?

<p>I&#39;ve got a json file that I&#39;m basically using as a database for this little todolist program I&#39;m writing for myself. My question is, what would be the best way to write to the file, and modify its contents? Currently I&#39;ve got a suitable struct that holds all the contents of todos, and I&#39;d like to be ...阅读全文

资源 2016-12-21 09:00:22 blov

Handle url with parameters

<p>Is there a mux that handles functions with extra parameters? I don&#39;t want to do the boilerplate code to convert MyHandler to http.Handler if I can avoid it. This code would probably be similar to how Spring Boot controllers and Django urldispatchers work. </p> <pre><code>package main import ( &#34;custom...阅读全文

资源 2017-04-20 22:00:02 blov

Can golang package source with *no imports* be assumed “safe”? (AKA auditing golang with `grep import`)

<p>TL;DR- Can we audit golang file providing function supposed to have no side-effects with just <code>grep import</code>?</p> <p>Given golang source file, providing functions, structures, that does not import anything, can I assume that this package works with no other side-effects than within package scope, cpu and ...阅读全文

资源 2018-01-16 00:30:11 blov

Help with understanding this Struct/Inheritance problem in golang?

<p>Hello all, I am using Go to do homework in a database class rather than my usual Python as a way to learn Go. So I&#39;ve learned how to scan data from a query directly into a struct using sqlx, but I have a question on the structs themselves. The example database has these tables: Pets, Customers, Visits, Owner History. (It&#39;s ...阅读全文

Is there a package like a http router, but for generic purposes or sockets?

<p>I am working on a project that uses sockets for communication between multiple clients and a server. The sockets are used, because the clients can make a request to the server to perform a certain multi-step action and the server must provide feedback to the client after each step is completed. Essentially it is still similar to an http AP...阅读全文

资源 2017-08-22 13:08:56 agolangf

For loop structure

<p>Hey guys,</p> <p>I was going through <a href="https://github.com/golang/tour/blob/master/solutions/loops.go" rel="nofollow">this solution</a> from the Go Tour and saw this snippet of code:</p> <pre><code>for math.Abs(n-z) &gt; delta { n, z = z, z-(z*z-x)/(2*z) } </code&...阅读全文

资源 2017-08-30 14:00:14 polaris

Error Coverage

<p>Do you guys test for coverage when you pass an error along?</p> <p>For a specific example say I&#39;m using database/sql with a driver and I know the driver returns errors. I want to pass those errors along to the caller but I feel that testing these is really testing the driver implementation. </p> <p>Am I wr...阅读全文

资源 2017-05-05 22:00:02 blov

Does anyone know of a resource that explains boltdb internals? A walkthrough of the code base?

<p>I&#39;m looking to get a better grasp on how boltdb works under the hood. Reading the source is great but I&#39;m looking for something to help understand some of the concepts/rationale behind the code. </p> <p>A walkthrough type piece is what I&#39;m looking for - but any lower level explanations would be great to...阅读全文

资源 2017-12-20 16:00:01 blov

Largest Go codebase

<p>Does someone know which is the largest opensource go codebase in existence?</p> <hr/>**评论:**<br/><br/>Kraigius: <pre><p>Probably Go repo itself, no?</p> <p><a href="https://github.com/golang/go" rel="nofollow">https://github.com/golang/go</a></p></pre>...阅读全文

资源 2018-01-03 22:30:32 blov

Marshal/Unmarshal struct with bit fields and variable length fields

<p>I&#39;m working on a library for handling a wire protocol that sends specific messages where elements could be represented by bits in a byte, a fixed byte or word, or even a variable array of bytes. The methods in encoding/binary sounds great for my purpose except they only work with fixed length fields. It is important for my project ...阅读全文

资源 2017-02-06 13:00:10 blov

使用 gorilla/mux 增强 Go HTTP 服务器的路由能力

今天这篇文章我们将会为我们之前编写的HTTP服务器加上复杂路由的功能以及对路由进行分组管理。在之前的文章《深入学习用 Go 编写HTTP服务器》中详细地讲了使用net/http进行路由注册、监听网络连接、处理请求、安全关停服务的实现方法,使用起来非常方便。但是net/http有一点做的不是非常好的是,它没有提供类似URL片段解析、路由参数绑定这样的复杂路由功能。好在在Go社区中有一个非常流行的gorilla/mux包,它提供了对复杂路由功能的支持。在今天这篇文章中我们将探究如何用gorilla/mux包来创建具有命名参数、GET/POST处理、分组前缀、限制访问域名的路由。 安装gorilla/mux包 我们在之前写的HTTP服务的代码根目录,使用go get命令从GitHub安装软件包,如...阅读全文

博文 2020-02-06 10:34:19 kevinyan

让我们一起啃算法----移除元素

移除元素(Remove-Element) 题干如下: 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。示例 1: 给定 nums = [3,2,2,3], val = 3, 函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。 你不需要考虑数组中超出新长度后面的元素。示例 2:给定 nums = [0,1,2,2,3,0,4,2], val = 2,函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意顺序。你不需...阅读全文

博文 2020-05-07 10:32:47 三斤和他的朋友们

Analyzing QUIC Traffic

<p>Was wondering if anyone had any experience with analyzing the traffic coming from QUIC? If so besides using WireShark, and anything else see QUIC decrypted over the wire?</p> <hr/>**评论:**<br/><br/>knotdjb: <pre><p>If you&#39;re feeling up to it, you could write a dissector using <a href="https...阅读全文

资源 2016-12-15 20:00:12 agolangf

The conventional FizzBuzz problem in go Lang using Maps.

<p>I am learning go and was curious on how to use go maps to solve the conventional fizzbuzz problem, i.e. print number from say, 1-100 and multiples of 3 &amp; 5 get printed fizz and buzz respectively. I looked over a bunch of solutions in different languages but somehow can&#39;t produce the same results in go. Thanks in advance.<...阅读全文