Go: integer overflows
Christophe Colombier

Christophe Colombier @ccoveille

About: Smiling person, father of two, Husband, Senior Developer/Architect (in that exact order, it's important) Experience in development since 2004 Linux user and advocate since 2001

Location:
Villeurbanne, France
Joined:
Aug 10, 2022

Go: integer overflows

Publish Date: Sep 10 '24
0 2

Let's continue my articles about integer overflows in Go.

Here are examples

var a int
var b uint8

a = 255 + 1
b = uint8(a)
fmt.Println(b) // 0 conversion overflow

a = -1
b = uint8(a)
fmt.Println(b) // 255 conversion overflow

c, d := 255, 300
res := int8(max(c, d))
fmt.Println(res) // 44

str := "\x99"             // hexadecimal representation of Trademark ASCII character: ™
fmt.Println(str[0])       // 153
fmt.Println(int8(str[0])) // -103
Enter fullscreen mode Exit fullscreen mode

Go Playground

Comments 2 total

  • Hugo Chargois
    Hugo ChargoisSep 24, 2024

    Small correction: the trademark character is not in ASCII since ASCII is only 7-bit. The string "\x99" is not a valid UTF-8 encoding either; so it is simply a string that contains the byte 153

Add comment