Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang Why aren't these two strings equal?

Tags:

go

I copied and pasted these two strings (one from a Google Doc and one from terminal) – what the heck is going on? And how can I clean them up so they are the same?

package main

import "fmt"

func main() {
    fmt.Println([]byte("f6f77482e4394a21815b7090bc0185b4"))
    fmt.Println([]byte("f6f77482­e439­4a21­815b­7090bc0185b4"))
}

Returns:

[102 54 102 55 55 52 56 50 101 52 51 57 52 97 50 49 56 49 53 98 55 48 57 48 98 99 48 49 56 53 98 52]
[102 54 102 55 55 52 56 50 194 173 101 52 51 57 194 173 52 97 50 49 194 173 56 49 53 98 194 173 55 48 57 48 98 99 48 49 56 53 98 52]

Which are clearly two different byte arrays for the same string.

https://play.golang.org/p/_zd7tjqCZl

like image 553
K2xL Avatar asked Mar 12 '16 03:03

K2xL


People also ask

How do you compare two strings in Go?

Using Compare() method: You can also compare two strings using the built-in function Compare() provided by the strings package. This function returns an integer value after comparing two strings lexicographically. The return values are: Return 0, if str1 == str2.

Are strings copied by value Golang?

In go, everything is pass by value. A string is a struct that has a length and a pointer to a byte array. When you pass a string to another function, it copies the length and the pointer. As a consequence, the new copied string points to the same underlying data.


1 Answers

The second one has a number of "soft hyphen" (U+00AD) characters between the visible characters, the first one appearing between "482" and "e4". A soft hyphen is a character that's invisible unless it happens to be at the location of a line-break, then it appears as a hyphen. Did you copy-paste the code from a word processor or some other program that might have applied special text formatting to it?

like image 62
hobbs Avatar answered Sep 17 '22 15:09

hobbs