Billion-Dollar Mistake in Go?

Billion-Dollar Mistake in Go?

What good comes with interfaces if we really cannot hide the implementation details with them? The interface should set its semantics, not the ...

The following sample code is from Go's standard library documentation:

data := make([]byte, 100)
count, err := file.Read(data)
if err != nil {
fmt.Printf("read %d bytes: %q\n", count, data[:count])

It seems to be ok. It must be correct because it's from the official documentation of the standard library, right?

Let's spend a few seconds to figure out what's wrong with it before reading the documentation of the 

io.Readerwhich declares theReadfunction.


ifstatement in the sample should have been written like this (at least):

if err != nil && err != io.EOF {

Did I trick you (and my self)? Why didn’t we check the 

File.Readfunction’s documentation? Isn’t it the correct one? Well, it shouldn’t be the only one.1

What good comes with interfaces if we really cannot hide the implementation details with them? The interface should set its semantics, not the implementer as 

File.Read did. What happens to the code above when interface implementer is somethings else than File, but it still is io.Reader? It exits too early when it returns data and io.EOF together, which is allowed for all io.Reader implementers.1

Interface vs Implementer

In Go, you don’t need to mark an implementer of the interface explicitly. It’s a powerful feature. But does it mean that we should always use interface semantics according to the static type? For example, should the following 

Copy function use io.Reader semantics?

func Copy(dst Writer, src Reader) (written int64, err error) {
    src.Read() // now read semantics come from io.Reader?

But should this version use only 

os.File semantics? (Note, these are just dummy examples.)

golang go error-handling interface programming standard-library package coding

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Golang Interface Example | Interface Implementation in Go

Golang interface example | Interface Implementation in Go. An interface is type defined using set of method signatures and implment using struct.

What's new in the go 1.15

Go announced Go 1.15 version on 11 Aug 2020. Highlighted updates and features include Substantial improvements to the Go linker, Improved allocation for small objects at high core counts, X.509 CommonName deprecation, GOPROXY supports skipping proxies that return errors, New embedded tzdata package, Several Core Library improvements and more.

Error Handling in Golang

Exploring Error Handling Patterns in Golang .In this blog post, we’ll take a look at the best practices that could be used to handle errors in a Go application. A basic understanding of how Go works is all that is required to digest this article

How to Error Handling in Go Programs

Go does not have exceptions, rather error handling is done by checking errors. This requires some adjustments in how you deal with errors if you are coming from languages that have exceptions as a primary error handling mechanism.

Error Handling in Go Programs

We discuss effective ways to handle errors in Go programming language.