Code With Go
  • Code With Go
  • 01. What, Who, Why and Where?
  • 02. Compiled vs Interpreted languages
  • 03. Types of Type!
  • 04. Stack and Heap Memory
  • 05. Garbage Collection
  • 06. About Go
  • 07. Go Playground
  • 08. Hello, World!
  • 09. Installing Go
  • 10. Variables
  • 11. Constants
  • 12. Integers
  • 12. Floats
  • 13. Boolean
  • 14. Strings
  • 15. Complex Numbers
  • 16. If Else
  • 17. Switch
  • 18. For
  • 19. User Defined Types
  • 20. Arrays
  • 21. Slices
  • 22. Structs
  • 23. Maps
  • 24. Functions
  • 25. Defer
  • 26. Pointers
  • 27. Methods
  • Resources
    • Resources - Paid
    • Resources - Free
Powered by GitBook
On this page
  • String Types
  • Declaring String
  • Length of string
  • Sub-string operation
  • Blog Posts

Was this helpful?

14. Strings

String Types

String Types are used to store text like data.A string is an immutable sequence of bytes.

Go uses UTF-8 encoding to store strings.

A character in UTF8 can be from 1 to 4 bytes long. UTF-8 can represent any character in the Unicode standard. UTF-8 is backwards compatible with ASCII. UTF-8 is the preferred encoding for e-mail and web pages - W3Schools.com

Declaring String

Strings are declared using one of the following ways:

main.go
	var str1 string = "Hello, World!"
	var str2 = "Hello, World!"
	str3 := "Hello, World!"

Zero value for string is empty string ""

Length of string

Build in len function can be used to get number of bytes in string

main.go
fmt.Printf("length of str1 is %d \n", len(str1))

Sub-string operation

We can easily create substring from existing string using s[i,j].This operation always generates new string since string are immutable. i indicates starting index for substring including i and j indicates end index excluding j

main.go
str4 := str1[7:13]

fmt.Println(str4)

Blog Posts

Previous13. BooleanNext15. Complex Numbers

Last updated 5 years ago

Was this helpful?

Strings, bytes, runes and characters in Go - The Go Programming Language
Logo