Creating a sting of particular size?

(akhil) #1

We can create a byte array ([5]byte) of fixed size.
Is there any way to create a fixed size for the string?
I know that string Max size is 2^31 - 1 and this will be the size if we create a variable of string datatype.

But I just wanted to know can we create a string of 20 bytes or 30 bytes.


(Johan Dahl) #2

Hi. What do you want to use it for? It’s maybe easier to answer the question then.

(akhil) #3

Suppose if have a string of 1000 characters and user wants to get only 100 characters of string at first call.
He will create a string of 100 bytes(suppose if 1 char occupies 1 byte). Then I have to find out the size of the string(which user has created) and the pass only that many characters.

Here 100 is just an example user can create a string of size as they want.

Hope this example will make some sense.

(Jay Ts) #4

Hi Akhil,

First, please look at how strings are defined in Go:

So by definition, strings have fixed sizes.

The way your example is usually handled in Go is to use a slice. If you need a string type, you can easily convert a slice of bytes into a string.

(akhil) #5

Thanks @jayts

Using slices we can do it as I mentioned in my first post itself. But I was looking to do using string. So from your link, we cannot do it with strings as they are fixed size.

(Yamil Bracho) #6

You can also check strings.Builder

(Jay Ts) #7

You don’t have to use a slice. Try this:

(Johan Dahl) #8


I’ts more complex then this. Strings in go is by default encoded in utf8 so if you just take the first 100 bytes can you end up with an illegal string if your string contains anything but ascii (7 bit) characters. Utf-8 can represent single character with 1 up to 4 bytes (see

(Johan Dahl) #9

See here

Just taking the first two bytes of the string “Räksmörgås” (shrimp sandwich in Swedish) results in an illegal string because the letter ä (a umlaut) takes two bytes. But if you convert the string to runes and pick the first 2 and convert them back to a string it will be “Rä”

(akhil) #10

This is different from my question. Here you creating a string with value. But I declare a empty string and then get the size of that.

var a string
//Here the  string size is 2^31-1
// I am trying to change the size of the declared string so that its declaration size will be 10bytes or 20 bytes

Hope this will give you enough info on my post.

(akhil) #11

Hi @johandalabacka

From your example, I see that the user has to pass the number of characters(n) he wants.
So the user will get a string of n characters.


(Norbert Melzer) #12

You need to limit the size on your own using runtime checks, there is no way to restrict a strings length at compile time.

(akhil) #13


(Johan Dahl) #14

I had a small bug in my example. If the string is shorter than the wanted character it could panic with a slice index out of bounds. Corrected:

(Johan Dahl) #15

To make it faster use a StringBuilder and just fill it with the first n runes

func Left(s string, n int) string {
	sb := strings.Builder{}
	for i, r := range s {
		if i >= n {
	return sb.String()

(cosmos) #16

Maybe same for Romanian language. We have diacritics.

For example, my name contains Ionuț

(cosmos) #17

Maybe you can use this

(Johan Dahl) #18

Exactly all letters which isn’t in the American alfabet A-Z and a-z will require at least 2 bytes (up to 4)