O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

There's more...

Just for comparison, there is a benchmark code, which compares the performance of the built-in + operator, bytes.Buffer, and built-in copy:

  1. Create a bench folder and file bench_test.go in it with the following content:
        package main        import (          "bytes"          "testing"        )        const testString = "test"        func BenchmarkConcat(b *testing.B) {          var str string          b.ResetTimer()          for n := 0; n < b.N; n++ {            str += testString          }          b.StopTimer()        }        func BenchmarkBuffer(b *testing.B) {          var buffer bytes.Buffer          b.ResetTimer()          for n := 0; n < b.N; n++ {            buffer.WriteString(testString)          }          b.StopTimer()        }        func BenchmarkCopy(b *testing.B) {          bs := make([]byte, b.N)          bl := 0           b.ResetTimer()          for n := 0; n < b.N; n++ {            bl += copy(bs[bl:], testString)          }          b.StopTimer() } ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required