Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

String Compare in Go

strings.compare function is used to compare two strings. When both strings are equal the function returns 0. When first string (in ASCII value) is less than second than second string then the strings.compare returns -1. And when first string is greater than second it returns 1 as output.

strings.compare is case sensitive i.e when a and A are compared strings.compare returns 1.

package main

import (
    "fmt"
    "strings"
)

func main() {

    compareScore := strings.Compare("a", "a") // equal
    fmt.Printf("%d\n", compareScore)

    compareScore = strings.Compare("a", "b") // ascii less than second
    fmt.Printf("%d\n", compareScore)

    compareScore = strings.Compare("bb", "ba") // ascii greater than second, first character is same so ignored
    fmt.Printf("%d\n", compareScore)
}