Replaces all but the last n of characters with the specified mask character.

Slice the given string appropriately, using strings.Repeat() to add n times m to the beginning.

代码实现

import "strings"

func Mask(cc string, n int, m rune) string {
    return strings.Repeat(string(m), len(cc)-n) + cc[len(cc)-n:]
}

使用样例

Mask("1234567890", 4, '*') // "******7890"