Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot use type []rune as type rune in append

Tags:

append

go

rune

package main

var lettersLower = []rune("abcdefghijklmnopqrstuvwxyz")
var lettersUpper = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")

func main() {
    x := append(lettersLower, lettersUpper)
}

Why does this not work? How can I append lettersLower and lettersUpper?

prog.go:7: cannot use lettersUpper (type []rune) as type rune in append

https://play.golang.org/p/ovx_o2rKPC

like image 204
fnkr Avatar asked Feb 20 '15 23:02

fnkr


People also ask

What is [] rune in Golang?

The Rune type is an alias of int32. Important Points: Always remember, a string is a sequence of bytes and not of a Rune. A string may contain Unicode text encoded in UTF-8.

How do you turn a rune into a string?

If you just want to convert a single rune to string , use a simple type conversion. rune is alias for int32 , and converting integer numbers to string : Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer.

What is rune datatype?

A rune is an alias to the int32 data type. It represents a Unicode code point. A Unicode code point or code position is a numerical value that is usually used to represent a Unicode character. The int32 is big enough to represent the current volume of 140,000 unicode characters.

How do you add a character to a string in go?

Using Sprintf: In Go language, you can also concatenate string using Sprintf() method. Using += operator or String append: In Go strings, you are allowed to append a string using += operator. This operator adds a new or given string to the end of the specified string.


1 Answers

It's because append doesn't take a list to append, but rather one or more items to append. You can adapt to this with a ... on the second argument to append:

package main

import "fmt"

var lettersLower = []rune("abcdefghijklmnopqrstuvwxyz")
var lettersUpper = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")

func main() {
    x := append(lettersLower, lettersUpper...)
    fmt.Println(len(x))
}

Try it out on the Playground.

Note that append does not always re-allocate the underlying array (that would cause problems in terms of performance and memory usage). You're fine as far as this sample goes, but it may bite you if you ever try to use the same memory for multiple purposes. A (contrived, perhaps unclear) example:

package main

import (
    "fmt"
    "os"
)

func main() {
    foo := []byte("this is a BIG OLD TEST!!\n")
    tst := []byte("little test")
    bar := append(foo[:10], tst...)

    // now bar is right, but foo is a mix of old and new text!
    fmt.Print("without copy, foo after:  ")
    os.Stdout.Write(foo)

    // ok, now the same exercise but with an explicit copy of foo
    foo = []byte("this is a BIG OLD TEST!!\n")
    bar = append([]byte(nil), foo[:10]...) // copies foo[:10]
    bar = append(bar, tst...)

    // this time we modified a copy, and foo is its original self
    fmt.Print("with a copy, foo after:   ")
    os.Stdout.Write(foo)
}

When you try to print foo after appending to a subslice of it, you get a weird mix of old and new content.

Where the shared underlying array is a problem, you could either use strings (string bytes are immutable, a pretty effective guard against accidental overwrites) or make a copy as I did with append([]byte(nil), foo[:10]...) above.

like image 145
twotwotwo Avatar answered Sep 25 '22 13:09

twotwotwo