Go 标准包 strings 学习(1) Clone,Compare,Contains
事先声明:理解部分都是自己的理解,可能有错误,仅供参考
学习go语言 1.20.3版本 标准包里面的函数
go语言标准包官网
https://pkg.go.dev/std
学习strings包的内容
官网
https://pkg.go.dev/strings@go1.20.3

func Clone
Clone returns a fresh copy of s. It guarantees to make a copy of s into a new allocation, which can be important when retaining only a small substring of a much larger string. Using Clone can help such programs use less memory. Of course, since using Clone makes a copy, overuse of Clone can make programs use more memory. Clone should typically be used only rarely, and only when profiling indicates that it is needed. For strings of length zero the string "" will be returned and no allocation is made.
翻译
Clone返回s的新副本。它保证将s的副本生成到新的分配中,这在只保留一个大得多的字符串的一小个子字符串时非常重要。使用克隆可以帮助这类程序使用更少的内存。当然,由于使用Clone会生成副本,因此过度使用Clone会使程序使用更多内存。克隆通常应该很少使用,并且仅在分析表明需要它时才使用。对于长度为0的字符串,将返回字符串"",并且不进行分配
理解
返回字符串的复制,可用于从一个很大的字符串截取出来一个小字符串,少用

func Compare
Compare returns an integer comparing two strings lexicographically. The result will be 0 if a == b, -1 if a < b, and +1 if a > b.
Compare is included only for symmetry with package bytes. It is usually clearer and always faster to use the built-in string comparison operators ==, <, >, and so on.
翻译
Compare返回一个按字典顺序比较两个字符串的整数。如果a == b,结果为0,如果a < b,结果为-1,如果a > b,结果为+1。
Compare仅用于与包字节对称。使用内置的字符串比较运算符==、<、>等通常更清晰,而且总是更快。
例子
理解
返回2个字符串比较大小,经过在官网上实验,会根据传入的2个字符串进行一个字符一个字符的比较,当对应位置的字符大小不一样的时候就直接返回结果,不管后面了
举例:
返回 -1
因为 第二个位置的 a 小于 b ,就算后面还有字符,也直接返回 -1
返回 1
因为 cb 的第3个位置没有值,cbc 有值,所以cbc > cb

func Contains
Contains reports whether substr is within s.
翻译
包含报告substr是否在s内。
理解
s 是否包含 substr ,包含返回true,不包含返回false
案例
true
false
true
true