Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Pick a random character in string in Go (Golang)

Posted on March 31, 2023March 31, 2023 by admin

Table of Contents

  • Overview
  • Code

Overview

‘mat/rand’ package of golang contains a Intn function that can be used to generate a pseudo-random number between [0,n). Bracket at the end means that n is exclusive. This function can be utilized to pick a random element in a string. We can generate a random between 0 and length-1 of string. Then we can use that random number to index into the string and get the result.

But there is one problem in above approach. In Golang string is a sequence of bytes. A string literal actually represents a UTF-8 sequence of bytes. In UTF-8, ASCII characters are single-byte corresponding to the first 128 Unicode characters. All other characters are between 1 -4 bytes. Due to this it is not possible to index a character in a string.  In GO, rune data type represents a Unicode point.  Once a string is converted to an array of rune then it is possible to index a character in that array of rune.

You can learn more the above issue here – /number-characters-string-golang/

For this reason in below program for picking a random in a given string , we  are first converting a string into a rune array so that we can index into the rune array and then return the random character.

Code

package main
import (
"fmt"
"math/rand"
)
func main() {
in := "abcdedf£"
inRune := []rune(in)
randomIndex := rand.Intn(len(inRune))
pick := inRune[randomIndex]
fmt.Println(string(pick))
}

Output:

One of a,b,c,d,e,f,£
  • character
  • go
  • golang
  • pick
  • random
  • string
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP