<p>I'm working through the book, <a href="http://a.co/2Yp8lEJ">The Go Programming Language</a> and came across a section in the chapter on interfaces that said the errors package was 4 lines of code. They showed the code in the following paragraph and I couldn't believe it so I went to the Go GitHub page to see for myself, and sure enough, besides the package comments and the formatting, all the code fits on 4 lines. I thought that was pretty awesome.</p>
<p>There's so little code I can reproduce it here too:</p>
<pre><code>// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package errors implements functions to manipulate errors.
package errors
// New returns an error that formats as the given text.
func New(text string) error {
return &errorString{text}
}
// errorString is a trivial implementation of error.
type errorString struct {
s string
}
func (e *errorString) Error() string {
return e.s
}
</code></pre>
<p>So, that lead me to wonder what else is there about Go that's like this. Are there similar other files that are so tiny, but are used almost everywhere and in all Go code? </p>
<hr/>**评论:**<br/><br/>blackflicker: <pre><p>Check out the io package, for example. The files there are relatively not tiny but the interfaces are. <code>io.Reader</code> and <code>io.Writer</code>.</p></pre>DeedleFake: <pre><p>I have to say, <code>io.Reader</code>, <code>io.Writer</code>, and their ubiquitous usage is one of my favorite things about Go.</p>
<p>Now if it was just a bit easier to use a blocking <code>Read()</code> call with a <code>select</code> and a channel...</p></pre>ESBDB: <pre><p>I mean the errors package just implements a basic implementation of the error interface, that's not really surprising. It would be more interesting to see the error interface definition, but that is baked into the language so not sure how it works.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传