Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 获取循环子字符串的最佳方法是什么_Go - Fatal编程技术网

Go 获取循环子字符串的最佳方法是什么

Go 获取循环子字符串的最佳方法是什么,go,Go,我想要像这样的东西: var peptide = "LENQ" peptide[2:3] -> NQ peptide[2:4] -> NQL peptide[2:5] -> NQLE 做这件事最好的方法是什么?可能有一个库函数来获取它,或者我需要自己编写它吗 例如 package main import ( "fmt" "unicode/utf8" ) func cyclicSubstrings(str string) []string { n

我想要像这样的东西:

var peptide = "LENQ"

peptide[2:3] -> NQ
peptide[2:4] -> NQL
peptide[2:5] -> NQLE
做这件事最好的方法是什么?可能有一个库函数来获取它,或者我需要自己编写它吗

例如

package main

import (
    "fmt"
    "unicode/utf8"
)

func cyclicSubstrings(str string) []string {
    n := utf8.RuneCountInString(str)
    substrs := make([]string, 0, n*n)
    cycles := str + str
    for i := range str {
        cycle := cycles[i : i+len(str)]
        for j, r := range cycle {
            substrs = append(substrs, cycle[:j+utf8.RuneLen(r)])
        }
    }
    return substrs
}

func main() {
    peptide := "LENQ"
    fmt.Println(cyclicSubstrings(peptide))
}
输出:

[L LE LEN LENQ E EN ENQ ENQL N NQ NQL NQLE Q QL QLE QLEN] [L LEN LEN LEN LENQ ENQ ENQ NQ NQ NQL NQL NQL QL ENQ NQL NQL NQL NQL NQL NQL NQL NQL NQL NQL NQL NQL NQL NQL NQL NQL