⚡️ Speed up development? The handy library of most used snippets for Go apps
Tags: go, showdev, productivity, opensource
Published: 2023-05-13 10:13:00

## Introduction

Well, hello, my DEV friends! 👋 

> It's been a long time since I had the time, and then the opportunity, to write this blog. But thank you, anyway, for staying with me and asking questions! 
> 
> I really appreciate it. Love you people! 😘

Today I'm going to tell you about my personal little project, which I put out to the public quite recently. But it helped me all these years to write Go programs as fast as possible and in full compliance with the DRY (Don't Repeat Yourself) principle.

Intrigued? Then welcome, it will be interesting! 👇

<a name="toc"></a>
### 📝 Table of contents

- [Motivation in library creation](#motivation-in-library-creation)
- [Basic use of the library](#basic-use-of-the-library)
- [Regular snippets](#regular-snippets)
- [Universal snippets](#universal-snippets)
- [Benchmarks](#benchmarks)
- [A win-win cooperation](#a-winwin-cooperation)

<a name="#motivation-in-library-creation"></a>
## Motivation in library creation

As you already know from my [previous articles](https://dev.to/koddr/series/4440), I take an approach to software development that makes the developer's life totally easy.

Why repeat the same snippet, for example, to translate a byte slice to a string, if you can add the most efficient solution to the library once and import it where you need it? Exactly right! It's an unnecessary cognitive load for those who will read your code in the future (and for you as well).

It is for these reasons that [The Go Snippet Library](https://github.com/koddr/gosl) (or `gosl` for a short) provides snippets collection for working with routine operations in your Go programs with a super user-friendly API and the most efficient [performance](https://github.com/koddr/gosl#%EF%B8%8F-benchmarks).

[↑ Table of contents](#toc)

<a name="#basic-use-of-the-library"></a>
## Basic use of the library

Yes, simply add `gosl` to your project:

```bash
go get github.com/koddr/gosl
```

Add the needed snippet to your code, like this:

```go
import (
   fmt
   log

   "github.com/koddr/gosl"
)

func main() {
    b := []byte("Hello, World!")

    s, err := gosl.ToString(b) // convert byte slice to string
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(s)
}
```

Or like this to have access to snippets as embedded struct:

```go
type App struct {
    // ...

    utils    *gosl.Utility                         // add regular snippets
    genUtils *gosl.GenericUtility[any, comparable] // add generic snippets
}

func (a *App) handleSomething(b []byte) error {
    // ...

    s, err := a.utils.ToString(b) // convert byte slice to string
    if err != nil {
        return err
    }

    // ...

    u, err := a.genUtils.Unmarshal(json, model) // unmarshal JSON data to struct
    if err != nil {
        return err
    }

    // ...
}
```

And you are already saving your time and energy. Trust me ✌️

[↑ Table of contents](#toc)


![gosl saving your time and energy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/752crte5p5c23cu67y8b.jpg)

<a name="#regular-snippets"></a>
## Regular snippets

The regular snippets of the [`gosl`](https://github.com/koddr/gosl) package are aimed at solving one single task with the smallest possible allocation of your machine's resources.

### Concat

Concatenate strings `s` to the one string:

```go
s1 := "this "
s2 := "is "
s3 := "my string"

s := gosl.Concat(s1, s2, s3)
```

### ContainsCaseInsensitive

Report if string `substr` is within string `s` (case-insensitive by default):

```go
s := "Hello, WORLD!"
substr := "r"

b := gosl.ContainsCaseInsensitive(s, substr)
```

### RandomString

Generates a (**really**) random string with a given size:

```go
size := 8

s, err := gosl.RandomString(size)
if err != nil {
    log.Fatal(err)
}
```

### ToString

Convert byte slice `b` to string or error:

```go
b := []byte("Hello, World!")

s, err := gosl.ToString(b)
if err != nil {
    log.Fatal(err)
}
```

### ToBytes

Convert string `s` to byte slice or error:

```go
s := "Hello, World!"

b, err := gosl.ToBytes(s)
if err != nil {
    log.Fatal(err)
}
```
[↑ Table of contents](#toc)

<a name="#universal-snippets"></a>
## Universal snippets

The universal (or _generic_) snippets of the [`gosl`](https://github.com/koddr/gosl) package are aimed at solving one particular task with the smallest possible allocation of your machine's resources, but can be applied to a huge number of user types.

### ContainsInSlice

Report if value `v` is within slice `s`:

```go
s := []string{"one", "two", "three"}
v := "two"

b := gosl.ContainsInSlice(s, v)
```

### ContainsInMap

Report if key `k` is within map `m`:

```go
m := map[string]int{"one": 1, "two": 2, "three": 3}
k := "two"

b := gosl.ContainsInMap(m, k)
```

### Marshal

Marshal struct `user` to JSON data `j` (byte slice) or error:

```go
type user struct {
    ID   int    `json:"id"`
    Name string `json:"name"`
}

u := &user{}

j, err := gosl.Marshal(u)
if err != nil {
    log.Fatal(err)
}
```

> 💡 This snippet is a 100% compatible drop-in replacement for the standard `encoding/json` library.

### Unmarshal

Unmarshal JSON data `j` (byte slice) to struct `user` or error:

```go
type user struct {
    ID   int    `json:"id"`
    Name string `json:"name"`
}

j := []byte(`{"id":1,"name":"Viktor"}`)
m := &user{}

u, err := gosl.Unmarshal(j, m)
if err != nil {
    log.Fatal(err)
}
```

> 💡 This snippet (also) is a 100% compatible drop-in replacement for the standard `encoding/json` library.

[↑ Table of contents](#toc)

<a name="#benchmarks"></a>
## Benchmarks

My results for all snippets in the [library](https://github.com/koddr/gosl) (the test stand is Apple MacBook Air M1, 16 GB RAM, macOS 13.3.1):

```bash
BenchmarkConcat_String2-8                       	58663996	        20.06 ns/op	      32 B/op	       1 allocs/op
BenchmarkConcat_String8-8                       	26829356	        44.16 ns/op	     128 B/op	       1 allocs/op
BenchmarkConcat_String32-8                      	 9321133	       127.8 ns/op	     448 B/op	       1 allocs/op

BenchmarkToString_HelloWorld-8                  	100000000	        10.56 ns/op	      16 B/op	       1 allocs/op

BenchmarkToBytes_HelloWorld-8                   	1000000000	         0.6288 ns/op	   0 B/op	       0 allocs/op

BenchmarkRandomString_Size1-8                   	 3488678	       344.6 ns/op	       6 B/op	       3 allocs/op
BenchmarkRandomString_Size8-8                   	 3394548	       353.3 ns/op	      24 B/op	       3 allocs/op
BenchmarkRandomString_Size64-8                  	 2313856	       517.9 ns/op	     160 B/op	       3 allocs/op
BenchmarkRandomString_Size512-8                 	 1423572	       838.9 ns/op	    1280 B/op	       3 allocs/op
BenchmarkRandomString_Size4096-8                	  185337	      6350 ns/op	   10240 B/op	       3 allocs/op

BenchmarkMarshal_StructField_4-8                	 8584442	       139.9 ns/op	      48 B/op	       3 allocs/op
BenchmarkMarshal_StructField_16-8               	 2838062	       420.8 ns/op	     192 B/op	       3 allocs/op

BenchmarkUnmarshal_StructField_4-8              	 6960462	       169.3 ns/op	      32 B/op	       3 allocs/op
BenchmarkUnmarshal_StructField_16-8             	  764182	      1553 ns/op	     864 B/op	      45 allocs/op

BenchmarkContainsCaseInsensitive_HelloWorld-8   	24856041	        48.46 ns/op	      16 B/op	       1 allocs/op
BenchmarkContainsCaseInsensitive_LoremIpsum-8   	 1797150	       695.9 ns/op	     448 B/op	       1 allocs/op

BenchmarkContainsInSlice-8                      	122999034	         9.758 ns/op	   0 B/op	       0 allocs/op

BenchmarkContainsInMap-8                        	19123504	        62.61 ns/op	       0 B/op	       0 allocs/op
```

[↑ Table of contents](#toc)

<a name="#a-winwin-cooperation"></a>
## A win-win cooperation

I invite every user of Dev.to (and my lowly blog, certainly) to participate in this project! Let's work *together* to create the largest and most useful library of snippets for Go programs on the web today.

- Ask questions and submit your features to the [Issues](https://github.com/koddr/gosl/issues) section.
- Send your snippets or improvements to the current to the [Pull requests](https://github.com/koddr/gosl/pulls) section.

Your PRs & issues are welcome! Thanks 😉

[↑ Table of contents](#toc)

<a name=""></a>
## Photos and videos by

- Marc Sendra Martorell https://unsplash.com/photos/-Vqn2WrfxTQ
- Vic Shóstak https://github.com/koddr

<a name=""></a>
## P.S.

If you want more articles (like this) on this blog, then post a comment below and subscribe to me. Thanks! 😻

And of course, you can help me make developers' lives even better! Just connect to one of my projects as a contributor. It's easy!

My projects that need your help (and stars) 👇

- 🔥 [gowebly][gowebly_url]: A next-generation CLI tool for easily build amazing web applications with Go on the backend, using htmx & hyperscript and the most popular atomic/utility-first CSS frameworks on the frontend.
- ✨ [create-go-app][cgapp_url]: Create a new production-ready project with Go backend, frontend and deploy automation by running one CLI command.
- 🏃 [yatr][yatr_url]: Yet Another Task Runner allows you to organize and automate your routine operations that you normally do in Makefile (or else) for each project.
- 📚 [gosl][gosl_url]: The Go Snippet Library provides snippets collection for working with routine operations in your Go programs with a super user-friendly API and the most efficient performance.
- 🏄‍♂️ [csv2api][csv2api_url]: The parser reads the CSV file with the raw data, filters the records, identifies fields to be changed, and sends a request to update the data to the specified endpoint of your REST API.
- 🚴 [json2csv][json2csv_url]: The parser can read given folder with JSON files, filtering and qualifying input data with intent & stop words dictionaries and save results to CSV files by given chunk size.

<!-- Footer links -->

[gowebly_url]: https://github.com/gowebly
[cgapp_url]: https://github.com/create-go-app
[yatr_url]: https://github.com/koddr/yatr
[gosl_url]: https://github.com/koddr/gosl
[csv2api_url]: https://github.com/koddr/csv2api
[json2csv_url]: https://github.com/koddr/json2csv