Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
50
Task/String-comparison/Go/string-comparison.go
Normal file
50
Task/String-comparison/Go/string-comparison.go
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Go language string comparison operators:
|
||||
c := "cat"
|
||||
d := "dog"
|
||||
if c == d {
|
||||
fmt.Println(c, "is bytewise identical to", d)
|
||||
}
|
||||
if c != d {
|
||||
fmt.Println(c, "is bytewise different from", d)
|
||||
}
|
||||
if c > d {
|
||||
fmt.Println(c, "is lexically bytewise greater than", d)
|
||||
}
|
||||
if c < d {
|
||||
fmt.Println(c, "is lexically bytewise less than", d)
|
||||
}
|
||||
if c >= d {
|
||||
fmt.Println(c, "is lexically bytewise greater than or equal to", d)
|
||||
}
|
||||
if c <= d {
|
||||
fmt.Println(c, "is lexically bytewise less than or equal to", d)
|
||||
}
|
||||
// Go is strongly typed and will not directly compare a value of string
|
||||
// type to a value of numeric type.
|
||||
|
||||
// A case insensitive compare can be done with a function in the strings
|
||||
// package in the Go standard library:
|
||||
eqf := `when interpreted as UTF-8 and compared under Unicode
|
||||
simple case folding rules.`
|
||||
if strings.EqualFold(c, d) {
|
||||
fmt.Println(c, "equal to", d, eqf)
|
||||
} else {
|
||||
fmt.Println(c, "not equal to", d, eqf)
|
||||
}
|
||||
|
||||
// Seeing that the built in operators work bytewise and the library
|
||||
// case folding functions interpret UTF-8, you might then ask about
|
||||
// other equality and inequality tests that interpret UTF-8.
|
||||
// Functions for this are not in the Go standard library but are in
|
||||
// the Go "sub repository" at golang.org/x/text. There is support
|
||||
// for Unicode normalization, collation tables, and locale sensitive
|
||||
// comparisons.
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue