Sling Academy
Home/Golang/Creating a Custom String Utility Package in Go

Creating a Custom String Utility Package in Go

Last updated: November 24, 2024

In this article, we will learn how to create a custom string utility package in Go. Packages provide a way for us to manage directories, compile fast, and avoid naming collisions. We'll start with basic string functions and move towards creating a more complex package.

Understanding Packages in Go

Before we dive into building our string utility, it's important to understand the concept of packages in Go. A package in Go is essentially a directory containing Go source files. All files within a package can import each other.

Basic Package Setup

First, let's create our package directory structure. Open a terminal and run the following commands:

$ mkdir -p $GOPATH/src/github.com/yourusername/stringutil
$ cd $GOPATH/src/github.com/yourusername/stringutil

Create a file named stringutil.go:

package stringutil

// Reverse returns a reversed string of input.
func Reverse(s string) string {
    r := []rune(s)
    for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
        r[i], r[j] = r[j], r[i]
    }
    return string(r)
}

Basic String Operations

The example function Reverse is a straightforward one that demonstrates reversing a string in Go. This will be a part of our utility package. Let’s step up and add more functionality.

Intermediate String Operations

Next, let's add a function to count vowels in a string:

package stringutil

// CountVowels counts the vowels in a given string.
func CountVowels(s string) int {
    count := 0
    vowels := "aeiouAEIOU"
    for _, char := range s {
        if strings.ContainsRune(vowels, char) {
            count++
        }
    }
    return count
}

Advanced String Conversions

Let’s add an advanced utility to check if a string is a palindrome:

package stringutil

// IsPalindrome checks if a given string is a palindrome.
func IsPalindrome(s string) bool {
    s = strings.ToLower(s)
    return s == Reverse(s)
}

Testing the Package

To ensure everything works, we should write some test cases using Go's testing package. Create a file named stringutil_test.go:

package stringutil

import "testing"

func TestReverse(t *testing.T) {
    input := "hello"
    want := "olleh"
    got := Reverse(input)
    if got != want {
        t.Errorf("Reverse(%q) = %q; want %q", input, got, want)
    }
}

func TestCountVowels(t *testing.T) {
    input := "education"
    want := 5
    got := CountVowels(input)
    if got != want {
        t.Errorf("CountVowels(%q) = %d; want %d", input, got, want)
    }
}

func TestIsPalindrome(t *testing.T) {
    input := "Madam"
    want := true
    got := IsPalindrome(input)
    if got != want {
        t.Errorf("IsPalindrome(%q) = %v; want %v", input, got, want)
    }

    input = "Hello"
    want = false
    got = IsPalindrome(input)
    if got != want {
        t.Errorf("IsPalindrome(%q) = %v; want %v", input, got, want)
    }
}

Run the tests:

$ go test

You should see an output indicating all tests passed successfully if everything was set up correctly. This confirms our package is functioning as intended.

Conclusion

In this tutorial, we learned to create a custom package in Go for string utilities. We built functions to reverse a string, count vowels, and check for palindromes, and tested our code. These skills are fundamental as they help build reusable components effectively in Go projects.

Next Article: Measuring String Similarity: Algorithms and Examples in Go

Previous Article: Sorting and Filtering Strings in Go

Series: Working with Strings in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant