doujiao8649 2019-01-08 12:40
浏览 218
已采纳

如何创建任意长度的字符串

I want to create a dummy string of a given length to do a performance test. For example I want to first test with 1 KB of string and then may be 10 KB of string etc. I don't care which character (or rune?) it gets filled with. I understand that a string in Go is backed by byte array. So, I want the final string to be backed by a byte array of size equivalent of 1 KB (if I give 1024 as the argument).

For example, I tried the brute force code below:

...
    oneKBPayload := createPayload(1024, 'A')
...
//I don't mind even if the char argument is removed and 'A' is used for example 
func createPayload(len int, char rune) string {
    payload := make([]byte, len)
    for i := 0; i < len; i++ {
        payload = append(payload, byte(char))
    }
    return string(payload[:])
}

and it produced a result of (for 10 length)

"\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000AAAAAAAAAA"

I realize that it has something to do with the encoding. But how to fix this so that I create any string which is backed by a byte array of the given length so that when I write it over the network, I generate the intended payload.

  • 写回答

1条回答 默认 最新

  • douhe5092 2019-01-08 12:44
    关注

    Your createPayload() creates a byte slice with the given length, which is filled with zeros by default (zero value). Then you append len number of runes to this slice, so the result will be double the length you intend to create (given the rune is less than 127), and that's why you see zeros then followed by the 'A' rune when printed.

    If you change it to:

    payload := make([]byte, 0, len)
    

    Then the result will be what you want.

    But easier would be to simply use strings.Repeat() which repeats a given string value n times. Repeat a one-rune (or more specifically a one-byte) string value n times, and you get what you want:

    s := strings.Repeat("A", 10)
    fmt.Println(len(s), s)
    

    This will output (try it on the Go Playground):

    10 AAAAAAAAAA
    

    If you don't care about the content of the string only about its length, then simply convert a byte slice like this:

    s := string(make([]byte, 1024))
    fmt.Println(len(s))
    

    Or alternatively like this:

    s2 := string([]byte{1023: 0})
    fmt.Println(len(s2))
    

    Both prints 1024. Try them on the Go Playground.

    If you do care about the content and you already have a byte slice allocated, this is how you can efficiently fill it: Is there analog of memset in go?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料