Skip to content

Instantly share code, notes, and snippets.

View renanbastos93's full-sized avatar
🏄‍♂️
Free times I go surfing usually

Renan Bastos renanbastos93

🏄‍♂️
Free times I go surfing usually
View GitHub Profile
@renanbastos93
renanbastos93 / error.go
Created December 30, 2023 22:39
Custom errors in Golang
// ./custom/error.go
package myerrors
import (
"fmt"
)
type myError struct {
code int
msg string
package main
import "testing"
func BenchmarkTestWithLength(b *testing.B) {
for i := 0; i < b.N; i++ {
useAppendWithLength()
}
}
func BenchmarkTestNotUseAppend(b *testing.B) {
for i := 0; i < b.N; i++ {
notUseAppend()
}
}
func BenchmarkTestWithoutLength(b *testing.B) {
for i := 0; i < b.N; i++ {
useAppendWithoutLength()
}
}
func BenchmarkTestWithLength(b *testing.B) {
for i := 0; i < b.N; i++ {
useAppendWithLength()
}
}
package main
var size = 10000
func useAppendWithoutLength() {
var arr = []int{}
for i := 0; i < size; i++ {
arr = append(arr, i)
}
}
func notUseAppend() {
var arr = make([]int, 1, size)
for k := range arr {
arr[k] = k
}
}
func useAppendWithLength() {
var arr = make([]int, 0, size)
for i := 0; i < size; i++ {
arr = append(arr, i)
}
}
func useAppendWithoutLength() {
var arr = []int{}
for i := 0; i < size; i++ {
arr = append(arr, i)
}
}
package main
import (
"fmt"
"time"
"github.com/gen2brain/beeep"
)
func main() {