Menu Close

Go – How to compare two strings in go golang ?

Here, We are going to learn about comparing two strings in go golang. We can compare two strings in go by using Compare() function of strings package in go golang.

Function proto type:

func Compare(a, b string) int

Return value:

Compare() function returns an integer value by comparing two strings lexicographically.

It returns 0 if a == b, -1 if a < b, and +1 if a > b.

Compare() function is included only for symmetry with package bytes and It is usually clearer and always faster to use the built-in string comparison operators ==, <, >, and so on.


package main

import (

func main() {

	fmt.Println(strings.Compare("Hello", "Hi"))

	fmt.Println(strings.Compare("Hello", "Hello"))

	fmt.Println(strings.Compare("Hi", "Hello"))




To learn more about golang, Please refer given below link.

Posted in golang, packages, strings

Leave a Reply

Your email address will not be published.

Contact Us