WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Go - Strings


Strings, that are extensively utilized in Go programming, are a readonly slice of bytes.

In the Go programming language, strings are slices. Go platform provides various libraries to manipulate strings.
  • unicode
  • regexp
  • strings

Creating Strings

The 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 world!'.
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"

Note − The string literal is immutable, so that once it is created a string literal cannot be changed.

String Length

len(str) technique returns the number 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 Strings

The strings package includes a method be part of for concatenating a couple of strings
strings.Join(pattern, " ")
Join concatenates the elements of an array to create a single string. Second parameter is seperator which is placed between element of the array.
package deal foremost

import ("fmt" "math" )"fmt" "strings")

func essential() 
   greetings :=  []string"Hello","international!"   
   fmt.Println(strings.Join(greetings, " "))
This might produce the following result
Hello global!