Go - Strings
Strings, that are extensively utilized in Go programming, are a readonly slice of bytes.In the Go programming language, strings are slices.
Creating StringsThe most direct manner to create a string is to write down
var greeting = "Hello world!"Whenever it encounters a string literal to your code, the compiler creates a string item with its value in this case, "Hello international!'.
A string literal holds a valid UTF-eight sequences known as runes. A String holds arbitrary bytes.bundle principal import "fmt" func main() var greeting = "Hello international Welookups!" fmt.Printf("normal string: ") fmt.Printf("%s", greeting) fmt.Printf("n") fmt.Printf("hex bytes: ") for i := 0; i < len(greeting); i++ fmt.Printf("%x ", greeting[i]) fmt.Printf("n") const sampleText = "xbdxb2x3dxbcx20xe2x8cx98" /*q flag escapes unprintable characters, with + flag it escapses non-ascii characters as well to make output unambigous */ fmt.Printf("quoted string: ") fmt.Printf("%+q", sampleText) fmt.Printf("n")This might produce the following result -$cross run primary.Cross normal string: Hello world Welookups! Hex bytes: forty eight sixty five 6c 6c 6f 20 77 6f seventy two 6c 64 20 fifty seven sixty five 6c 6f 6f 6b 75 70 seventy three 21 quoted string: "xbdxb2=xbc u2318"
String Lengthlen(str) technique returns the wide variety of bytes contained within the string literal.bundle important import "fmt" func main() var greeting = "Hello international Welookups!" fmt.Printf("String Length is: ") fmt.Println(len(greeting))This would produce the following result$pass run primary.Go String Length is: 22
Concatenating StringsThe strings package includes a method be part of for concatenating a couple of stringsstrings.Join(pattern, " ")Join concatenates the factors of an array to create a unmarried string. Second parameter is seperator which is placed among detail of the array. Let us have a look at the following examplepackage deal foremost import ("fmt" "math" )"fmt" "strings") func essential() greetings := string"Hello","international!" fmt.Println(strings.Join(greetings, " "))This might produce the following resultHello global!