OneBite.Dev - Coding blog in a bite size

count a string length in Go

Code snippet on how to count a string length in Go

  package main

  import "fmt"
  
  func main() {
  	str := "Go programming"
  
  	// Length of string
  	fmt.Println(len(str))
  }

This code is written in Go programming language. First, the package main is imported. Then a string is declared in a new variable called ‘str’. After that, len() function is used which returns length of the string. Finally, the print function is used to print the length of the string. The result will be 14 as there are 14 characters present in the string.

go