在Go中获得最多N个字符/元素的子字符串/子片段的简单方法

In Python I can slice a string to get a sub-string of up to N characters and if the string is too short it will simply return the rest of the string, e.g.

"mystring"[:100] # Returns "mystring"

What's the easiest way to do the same in Go? Trying the same thing panics:

"mystring"[:100] // panic: runtime error: slice bounds out of range

Of course, I can write it all manually:

func Substring(s string, startIndex int, count int) string {
    maxCount := len(s) - startIndex
    if count > maxCount {
        count = maxCount
    }
    return s[startIndex:count]
}

fmt.Println(Substring("mystring", 0, n))

But that's rather a lot of work for something so simple and (I would have thought) common. What's more, I don't know how to generalise this function to slices of other types, since Go doesn't support generics. I'm hoping there is a better way. Even Math.Min() doesn't easily work here, because it expects and returns float64.

Note that while a function remains the recommended solution (even if it has to be implemented for slices with different type), it wouldn't work well with string.

fmt.Println(Substring("世界mystring", 0, 5)) would actually print 世�� instead of 世界mys.
See "Code points, characters, and runes": a character may be represented by a number of different sequences of code points, and therefore different sequences of UTF-8 bytes.
And in Go, a "code point" is a rune (as seen here).

Using rune would be more robust (again, in case of strings)

func SubstringRunes(s string, startIndex int, count int) string {
    runes := []rune(s)
    length := len(runes)
    maxCount := length - startIndex
    if count > maxCount {
        count = maxCount
    }
    return string(runes[startIndex:count])
}

See it in action in this playground.