If you’re moving from another language (like Ruby or Python), there are a few language differences to learn, many of which revolve around handling strings. Here are some string tricks that solved some of the problems I had with Golang when I first started using it.

Multiline string

Creating a multi-line string in Go is simple. You only need to use (‘) when declaring or assigning.

str := `This is a
multiline
string.`
Copy the code

Run up: play.golang.org/p/mpIQuNhC1…

Note – whatever is indented in the string is retained in the final result.

str := `This string will have tabs in it`
Copy the code

Run up: play.golang.org/p/mpIQuNhC1…

Efficient string concatenation

Go 1.10 has a new string generatorstrings.Builder !

While the code below is still valid, Go 1.10 introduces the Builder type in the Strings package, which is easier to use and generally more efficient. I mentioned how to use it in this article, concatenating and building strings in Go 1.10+.

While Go allows strings to be concatenated using the + operator, this can become very inefficient when dealing with large numbers of string concatenations. It is more efficient to use the bytes.buffer concatenation character, which concatenates everything into a string at once.


import (
	"bytes"
	"fmt"
)

func main(a) {
	var b bytes.Buffer

	for i := 0; i < 1000; i++ {
		b.WriteString(randString())
	}

	fmt.Println(b.String())
}

func randString(a) string {
	// Pretend to return a random string
	return "abc-123-"
}
Copy the code

Run up: play.golang.org/p/mpIQuNhC1…

You can also use the strings.Join method if you prepare all the strings in advance.


import (
	"fmt"
	"strings"
)

func main(a) {
	var strs []string

	for i := 0; i < 1000; i++ {
		strs = append(strs, randString())
	}

	fmt.Println(strings.Join(strs, ""))}func randString(a) string {
	// Pretend to return a random string
	return "abc-123-"
}
Copy the code

Run up: play.golang.org/p/mpIQuNhC1…

Converts an integer (or other type) to a string type

In many languages, any data type can be converted to a string simply by concatenating it with a string or inserting a string (for example, “ID=#{ID}” in Ruby). Unfortunately, if you try to do this in Go, such as forcing an integer to be converted to a string, you won’t get the result you want.

i := 123
s := string(i)
Copy the code

What do you expect the output of S to be? If you guessed “123” like most people, you are dead wrong. Instead, you get something like “E” for the value of s. That’s not what we want at all!

Instead, you should use a package like Strconv or a function like fmt.sprintf. For example, here is one that uses strconv.itoa to convert integers to strings.

package main

import (
	"fmt"
	"strconv"
)

func main(a) {
	i := 123
	t := strconv.Itoa(i)
	fmt.Println(t)
}
Copy the code

Run up: play.golang.org/p/mpIQuNhC1…

You can also use the fmt.sprintf function to convert almost any data type to a string, but it should generally be reserved for instances where the string being created contains embedded data, rather than for converting a single integer to a string.

package main

import "fmt"

func main(a) {
	i := 123
	t := fmt.Sprintf("We are currently processing ticket number %d.", i)
	fmt.Println(t)
}
Copy the code

Run up: play.golang.org/p/mpIQuNhC1…

Fmt.sprintf operates almost the same as fmt.printf, except that instead of printing the resulting string to standard output, it returns it as a string.

limitSprintfThe use of

As I mentioned earlier, fmt.sprintf should generally be reserved for creating strings with embedded values. There are several reasons, but one that stands out is that FMt.sprintf doesn’t do any type checking, so it’s unlikely to find any errors until you actually run the code.

Fmt.sprintf is also slower than most functions typically used in strconv packages, though to be honest, the speed difference is so small that it’s usually not worth considering.

Creating random strings

This isn’t really a “quick trick,” but I find it’s a question I get asked a lot.

“How do I create random strings in Go?

Sounds simple enough. Many languages like Ruby and Python provide helpers that make generating random strings very easy, so Go must have one, right? Hey, hey, wrong.

Go chooses to provide only tools for creating random strings and leaves the details up to the developer. While this may feel uncomfortable at first, the benefit is that you have complete control over how strings are generated. This means you can specify the character set, how to seed random generation, and any other relevant details. In short, you have more control, but at the cost of writing some extra code.

Here is a quick example of using a Math/RAND package and a set of alphanumeric characters as a character set.


import (
	"fmt"
	"math/rand"
	"time"
)

func main(a) {
	fmt.Println(RandString(10))}var source = rand.NewSource(time.Now().UnixNano())

const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

func RandString(length int) string {
	b := make([]byte, length)
	for i := range b {
		b[i] = charset[source.Int63()%int64(len(charset))]
	}
	return string(b)
}
Copy the code

Run up: play.golang.org/p/mpIQuNhC1…

Go Playground always prints the same string

If you run this code multiple times on Go Playground, you might notice that it always outputs the same string -ajFLA7xPH5.

This is because Go Playground always uses the same time, so when we use rand.NewSource and pass the value at the current time is always the same, so the string we generate will always be the same.

For your particular needs, there may be a better solution than this, but it’s a good place to start. If you’re looking for ways to improve/change your code, you might consider using the Crypto/RAND package to generate random data – this is usually more secure, but may end up requiring more work.

Whatever you end up using, this example should help you get started. It works fine for most real-world use cases that don’t involve sensitive data, such as passwords and authentication systems. Be sure to remember to seed your random number generator! This can be done in the Math/RAND package through the rand.seed function, or by creating source code. In the example above is the way source code is created.

stringsPackages,HasPrefixAnd custom code

When working with strings, it is common to want to know whether a string begins or ends with a particular string. For example, if the API keys all start with sk_, you might want to verify that all the API keys provided in the API request start with that prefix, otherwise it would be too time-consuming to look up the data.

For functions that sound like very common use cases, you’d be better off accessing the String package directly and checking to see if it helps you solve the problem. In this case, you might use strings.HasPrefix(STR, prefix) and strings.HasSuffix(STR, prefix). Take a look at this example:

package main

import (
	"fmt"
	"strings"
)

func main(a) {
	fmt.Println(strings.HasPrefix("something"."some"))
	fmt.Println(strings.HasSuffix("something"."thing"))}Copy the code

Run up: play.golang.org/p/mpIQuNhC1…

While there are many useful generic functions in the Strings package, it’s worth noting that it’s not always worth finding a package that meets your needs. If you’ve used another language and started using Go, a common mistake is that developers spend too much time looking for packages that provide the functionality they need when they could easily write code to implement it themselves.

There are definitely benefits to using standard libraries; For example, they are thoroughly tested and well documented. Despite these benefits, if you find yourself spending more than a few minutes looking for a function, it’s equally beneficial to write it yourself. In this case, coding to your own needs will be done quickly, and you’ll be fully aware of what’s going on and won’t be caught off guard by strange borderline cases. You don’t have to worry about other people maintaining your code.

Strings can be converted to byte fragments (and vice versa)

Comments on Reddit pointed out that the correlation between strings and byte slices is not always obvious, so while this article was originally five tips, it has now been expanded to six.

In Go, you can convert a string to a byte slice ([]byte) and a byte slice to a string. Doing so is simple enough to look like any other type conversion. This conversion is usually done to pass a string to a function that accepts a byte slice, or to pass a byte slice to a function that requires a string. Here is an example of a conversion:

package main

import "fmt"

func main(a) {
	var s string = "this is a string"
	fmt.Println(s)
	var b []byte
	b = []byte(s)
	fmt.Println(b)
	for i := range b {
		fmt.Println(string(b[i]))
	}
	s = string(b)
	fmt.Println(s)
}
Copy the code

Run up: play.golang.org/p/mpIQuNhC1…

These are some tips for using strings in Go. I hope they will help you. If you need more go-related practices, check out the other tutorials I’ve published.

www.calhoun.io/6-tips-for-…