character Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/character/ Thu, 02 Apr 2020 07:03:46 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 character Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/character/ 32 32 159787465 Generate a random character in Go (Golang) https://vikasboss.github.io/generate-random-character-golang/ https://vikasboss.github.io/generate-random-character-golang/#respond Thu, 02 Apr 2020 07:02:14 +0000 https://vikasboss.github.io/?p=1933 Overview ‘mat/rand’ package of golang contains an Intn function that can be used to generate a random number between [0,n). The bracket at the end means that n is exclusive. To know...

The post Generate a random character in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

‘mat/rand’ package of golang contains an Intn function that can be used to generate a random number between [0,n). The bracket at the end means that n is exclusive.

To know more about what pseudo-random number means, check out this post – https://vikasboss.github.io/generate-random-number-golang

Below is the signature of this method. It takes input a number n and will return a number x in range 0<=x<n.

func Intn(n int) int

The above function can also be used to generate a random character too. See below program, it is used to generate a character. We are also providing a seed value to the rand so that it generates different output. It is used to generate:

  • Random character between lowercase a to z
  • Random character between uppercase A and Z
  • Random character between uppercase A and Z  and lowercase a to z

Code

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    rand.Seed(time.Now().Unix())

    //Generate a random character between lowercase a to z
    randomChar := 'a' + rune(rand.Intn(26))
    fmt.Println(string(randomChar))

    //Generate a random character between uppercase A and Z
    randomChar = 'A' + rune(rand.Intn(26))
    fmt.Println(string(randomChar))

    //Generate a random character between uppercase A and Z  and lowercase a to z
    randomInt := rand.Intn(2)
    if randomInt == 1 {
        randomChar = 'A' + rune(rand.Intn(26))
    } else {
        randomChar = 'a' + rune(rand.Intn(26))
    }
    fmt.Println(string(randomChar))
}

Output:

Will be lowercase between a to z
Will be uppercase between A to Z
Will be lowercase between a to z or uppsercase between A to Z

The post Generate a random character in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/generate-random-character-golang/feed/ 0 1933
Pick a random character in string in Go (Golang) https://vikasboss.github.io/pick-random-character-string-golang/ https://vikasboss.github.io/pick-random-character-string-golang/#respond Tue, 31 Mar 2020 15:59:17 +0000 https://vikasboss.github.io/?p=1918 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...

The post Pick a random character in string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
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 – https://vikasboss.github.io/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,£

The post Pick a random character in string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/pick-random-character-string-golang/feed/ 0 1918
Character in Go (Golang) https://vikasboss.github.io/character-in-go/ https://vikasboss.github.io/character-in-go/#respond Mon, 06 Jan 2020 16:52:28 +0000 https://vikasboss.github.io/?p=1107 Overview Golang does not have any data type of ‘char‘. Therefore byte is used to represent the ASCII character. byte is an alias for uint8, hence is of 8 bits or 1...

The post Character in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

Golang does not have any data type of ‘char‘. Therefore

  • byte is used to represent the ASCII character. byte is an alias for uint8, hence is of 8 bits or 1 byte and can represent all ASCII characters from 0 to 255
  • rune is used to represent all UNICODE characters which include every character that exists. rune is an alias for int32 and can represent all UNICODE characters. It is 4 bytes in size.
  • A string of one length can also be used to represent a character implicitly. The size of one character string will depend upon the encoding of that character. For utf-8 encoding, it will be between 1-4 bytes

To declare either a byte or a rune we use single quotes. While declaring byte we have to specify the type,  If we don’t specify the type, then the default type is meant as a rune.

To declare a string, we use double quotes or backquotes. Double quotes string honors escape character while back quotes string is a raw literal string and doesn’t honor any kind of escaping.

Code Example

See the program below. It shows

  • A byte representing the character ‘a
  • A rune representing the pound sign ‘£
  • A string having one character micro sign ‘µ’
package main

import (
    "fmt"
    "reflect"
    "unsafe"
)

func main() {
    //If you don't specify type here
    var b byte = 'a'
    
    fmt.Println("Priting Byte:")
    //Print Size, Type and Character
    fmt.Printf("Size: %d\nType: %s\nCharacter: %c\n", unsafe.Sizeof(b), reflect.TypeOf(b), b)
    
    r := '£'
    
    fmt.Println("\nPriting Rune:")
    //Print Size, Type, CodePoint and Character
    fmt.Printf("Size: %d\nType: %s\nUnicode CodePoint: %U\nCharacter: %c\n", unsafe.Sizeof(r), reflect.TypeOf(r), r, r)

    s := "µ" //Micro sign
    fmt.Println("\nPriting String:")
    fmt.Printf("Size: %d\nType: %s\nCharacter: %s\n", unsafe.Sizeof(s), reflect.TypeOf(s), s)
}

Output:

Priting Byte:
Size: 1
Type: uint8
Character: a

Priting Rune:
Size: 4
Type: int32
Unicode CodePoint: U+00A3
Character: £

Priting String:
Size: 16
Type: string
Character: µ

Caveats

  • Declaring a  byte with a NON-ASCII character will raise a compiler error as below. I tried with a character having a corresponding code as 285
constant 285 overflows byte
  • Only a single character can be declared inside a single quote while initializing byte or a rune. On trying to add two character between single quote, below compiler warning will be generated
invalid character literal (more than one character)

The post Character in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/character-in-go/feed/ 0 1107