
- Go - Home
- Go - Overview
- Go - Environment Setup
- Go - Program Structure
- Go - Basic Syntax
- Go - Data Types
- Go - Variables
- Go - Constants
- Go - Identifiers
- Go - Keywords
- Go - Operators
- Go - Arithmetic Operators
- Go - Assignment Operators
- Go - Relational Operators
- Go - Logical Operators
- Go - Bitwise Operators
- Go - Miscellaneous Operators
- Go - Operators Precedence
- Go Decision Making
- Go - Decision Making
- Go - If Statement
- Go - If Else Statement
- Go - Nested If Statements
- Go - Switch Statement
- Go - Select Statement
- Go Control Flow Statements
- Go - For Loop
- Go - Nested for Loops
- Go - Break Statement
- Go - Continue Statement
- Go - Goto Statement
- Go Functions
- Go - Functions
- Go - Call by Value
- Go - Call by Reference
- Go - Functions as Values
- Go - Function Closure
- Go - Function Method
- Go - Anonymous function
- Go Strings
- Go - Strings
- Go - String Length
- Go - String Concatenation
- Go - Compare Strings
- Go - Split String
- Go - Substring Extraction
- Go - String Replacement
- Go - String Interpolation
- Go - Parse Date Strings
- Go Arrays
- Go - Arrays
- Go - Multidimensional Arrays
- Go - Multidimensional Arrays
- Go - Passing Arrays to Functions
- Go - Pointers
- Go - Pointers
- Go - Array of pointers
- Go - Pointer to pointer
- Go - Passing pointers to functions
- Go Advanced Control Structures
- Go - Scope Rules
- Go - Dereferencing Pointer
- Go - Structures
- Go - Slice
- Go - Slice of Slices
- Go - Range
- Go - Maps
- Go - Recursion
- Go - Type Casting
- Go - Interfaces
- Go - Type Assertion
- Go - Error Handling
- Go - Concurrency
- Go - Regular Expression
- Go - Inheritance
- Go - Packages
- Go - Templates
- Go - Reflection
- Go - Generics
- Go File Handling
- Go - Read File By Word
- Go - Read File By Line
- Go - Read CSV Files
- Go - Delete File
- Go - Rename & Move File
- Go - Truncate a File
- Go - File Read-Write Mode W/O Truncation
- Go Miscellaneous
- Go - defer Keyword
- Go - Fmt Package
- Go - Zero Value
- Go - Import
Go - String Length
In Golang, a string is a sequence of characters. It is an immutable data type, meaning that once a string is created, it cannot be modified. Strings are enclosed in double quotes ("") and can contain any combination of letters, numbers, and symbols. They are commonly used to store text and are often used as input and output in programs.
String Length Using An External Function
In this method, the function that we create will accept the string whose length is to be calculated as an argument and returns the final length of the function in integer format.
Algorithm
- Step 1 First, we need to import the fmt package.
- Step 2 Then, we need to create a function named stringLength().
- Step 3 Create a for loop to iterate over the characters of the string and in every iteration it is incrementing the length variable.
- Step 4 Now, start the main() function.
- Step 5 Inside the main() initialize a variable of string data type and print it on the screen.
- Step 6 Now, call the stringLength() function by passing the string as an argument and store the result in a new variable.
- Step 7 Print this variable on the screen using fmt.Println() function.
Example
In this example, we will use an external user-defined function to find the length of a string by using an external function.
package main import "fmt" // Function to find the length of a string func stringLength(str string) int { var length int for range str { length++ } return length } func main() { // Initializing a string str := "Hello, world!" fmt.Println("The given string is:", str) var result int = stringLength(str) fmt.Println("Length of the above String is:", result) }
Output
The given string is: Hello, world! Length of the above String is: 13
String Length Using Internal Package
In this method, we will write a go language program to find the length of a string by using internal package.
Syntax
func RuneCountInString(s string) (n int)
The RuneCountInString() function is present in utf-8 package and is used to find the length of a string. This function accepts the string whose length is to be calculated as argument to a function and returns the length of it in integer.
func TrimSpace(str string) string
The trimSpace() function is present in strings package and is used to remove the spaces between the characters of a string. The function accept the required string as an argument and returns the final string obtained after removing the spaces between the words of it.
func len(v Type) int
The len() function is used to get the length of a any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.
func Count(s, sep []byte) int
The Count() function is present in bytes package and is used to calculate number of instances of a given slice. The function accepts two arguments one is the slice whose characters are to be counted and other is the separator whose occurrence is to be counted. The function then returns the integer value after counting the required characters.
Algorithm
- Step 1 First, we need to import the fmt and strings package.
- Step 2 Then, start the main() function.
- Step 3 Inside the main() function initialize a string type variable and store value to it.
- Step 4 Further, print the value on the screen using fmt.Println() function.
- Step 5 Now, use an internal function called TrimSpace() and pass the string as an argument to it.
- Step 6 Now, use the len() function to get the length of the resultant
- Step 7 print it on the screen by using fmt.Println() function.
Example 1
Go language example to find the length of a string by using strings package
package main import ( "fmt" "strings" ) func main() { // initializing a string str := "Prevention is better than cure" fmt.Println("The given string is:", str) var result int = len(strings.TrimSpace(str)) fmt.Println("Length of String:", result) }
Output
The given string is: Prevention is better than cure Length of String: 30
Example 2
In this example, we will use a go language program to find the length of a string by using the utf package.
package main import ( "fmt" "unicode/utf8" ) func main() { // Initializing string str := "The way to get started is to quit talking and begin doing" fmt.Println("The given string is:", str) var result int = utf8.RuneCountInString(str) fmt.Println("Length of String:", result) }
Output
The given string is: The way to get started is to quit talking and begin doing Length of String: 57
Example 3
In this example, we have used bytes package in order to write a go language program to find the length of a string.
package main import ( "bytes" "fmt" ) func main() { // Initializing string str := "The greatest glory in living lies not in never falling, but in rising every time we fall" fmt.Println("The given string is:", str) var result int = bytes.Count([]byte(str), nil) fmt.Println("Length of String:", result) }
Output
The given string is: The greatest glory in living lies not in never falling, but in rising every time we fall Length of String: 89