我应该如何在Golang中将切片转换为字符串?

我应该如何在Golang中将切片转换为字符串?

问题描述:

我正在分裂一个字符串。

我希望我的程序保留数组的一个元素并释放底层数组。

不幸的是,我不知道如何将字符串的一个片段转换为不引用底层字符串的字符串。

我是不是应该这样做:

func unslice(s string) (string) {
  return string([]byte(s))
}

背景是:

  1. 底层的字符串非常大
  2. 我想保留的部分很小
  3. 我想保留的部分会保留很长时间
  4. 这个程序会运行很长一段时间,甚至更长时间
  5. 在程序的生命周期中,它将分割这些字符串中的许多(数百万)

下面是一个回应评论的例子:

func takesBigStringOften(big string) {
    parts := strings.Split(big, " ")

    saveTinyStringForALongTime(parts[0])
}

我正在使用字符串。拆分以拆分字符串。 p>

I 希望我的程序保留数组的元素之一并释放基础数组。 p>

不幸的是,我无法弄清楚如何将字符串的片段转换为不包含字符串的字符串。 p>

我应该做这样的事情: p>

  func unslice(s string)(string  ){
返回字符串([] byte(s))
} 
  code>  pre> 
 
 

背景为: p>

    \ n
  1. 基础字符串很大 li>
  2. 我要保留的切片很小 li>
  3. 我要保留的切片将保留很长时间 li>
  4. 该程序将运行很长时间-数周或更长时间 li>
  5. 在该程序的生命周期内,它将拆分许多这样的字符串(百万) li> ol>

    以下是回应评论的示例。 p>

      func takeBigStringOften(big string){
     parts:=字符串 .Split(big,  “)
     
     saveTinyStringForALongTime(parts [0])
    } 
      code>  pre> 
      div>

To ensure that Go doesn't keep the underlying string in memory you will have to explicitly copy it to a new location:

func unslice(old string) string {
    new := make([]byte,len(old))
    copy(new,old)
    return string(old)
}

SmallString := unslice(BigString[0:7])

Just as some further information. Some benchmark code and memory profiling shows that as of go 1.5.3, both methods allocate the same amount of memory from the heap, i.e. a new copy is made either way. In building a string from a byte slice, the compiler calls a routine that makes a unique copy of the bytes - since strings are immutable and byte slices are not.

$ go tool pprof -alloc_space so002.test cprof0
Entering interactive mode (type "help" for commands)
(pprof) list copy
Total: 9.66MB
    9.62MB     9.62MB (flat, cum) 99.55% of Total
         .          .     15:
         .          .     16:var global string
         .          .     17:
         .          .     18:func benchmarkcopy(b *testing.B, c int) {
         .          .     19:   big := "This is a long string"
         .       240B     20:   parts := strings.Split(big, " ")
         .          .     21:   old := parts[0]
         .          .     22:   jlimit := 100
         .          .     23:   for i := 0; i < b.N; i++ {
         .          .     24:       for j := 0; j < jlimit; j++ {
    3.21MB     3.21MB     25:           global = string([]byte(old))
         .          .     26:       }
         .          .     27:       for j := 0; j < jlimit; j++ {
         .          .     28:           b := []byte(old)
    3.21MB     3.21MB     29:           global = string(b)
         .          .     30:       }
         .          .     31:       for j := 0; j < jlimit; j++ {
    3.21MB     3.21MB     32:           new := make([]byte, len(old))
         .          .     33:           copy(new, old)
         .          .     34:           global = string(old)
         .          .     35:       }
         .          .     36:   }
         .          .     37:}