'How to represent empty byte in Go

Want to have an empty char/byte, which has zero size/length, in Go, such as byte("").


func main() {
    var a byte = '' // not working
    var a byte = 0  // not working
}

A more specific example is


func removeOuterParentheses(S string) string {
    var stack []int
    var res []byte
    for i, b := range []byte(S) {
        if b == '(' {
            stack = append(stack, i)
        } else {
            if len(stack) == 1 {
                res[stack[0]] = '' // set this byte to be empty
                res[i] = '' // / set this byte to be empty
            }
            stack = stack[:len(stack)-1]
        }
    }
    return string(res)
}

There is an equivalent question in Java

go


Solution 1:[1]

a single byte is a number. 0 would transform into a 8bit number. 00000000.

A byte slice/array can have a length of 0.

var a byte = 0
var b = [0]byte{}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 piecepaper