2

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

dapangmao
  • 2,727
  • 3
  • 22
  • 18
  • 1
    "not working" meaning what? The zero value for `byte` is `0`. – Adrian Apr 16 '19 at 16:35
  • Though there's no need to explicitly assign the zero value, `var a byte` will get you a `byte` with its zero value by default. – Adrian Apr 16 '19 at 16:36
  • https://play.golang.org/p/7dVbK4iy6ts – Adrian Apr 16 '19 at 16:37
  • @Adrian the first one says `./main.go:4:16: empty character literal or unescaped ' in character literal`; the second one has length. – dapangmao Apr 16 '19 at 16:37
  • 1
    The first one is illegal, yes, the second is correct. Not sure what you mean it "has length". `byte` has no length property, it only has a value. – Adrian Apr 16 '19 at 16:38
  • If you want something with no value and a zero length, that is `struct{}` – Zan Lynx Apr 16 '19 at 16:39
  • 2
    Use `*byte` to differentiate between no byte and a zero byte. –  Apr 16 '19 at 16:40
  • Bear in mind that a `*byte` takes up 4-8 times as much memory as the `byte` it points to, which could be a problem in the aggregate. – Adrian Apr 16 '19 at 16:41
  • 5
    _"Want to have an empty char/byte in Go."_ Can you please clarify what do you mean by "empty"? – icza Apr 16 '19 at 16:58
  • @icza added more descriptions – dapangmao Apr 16 '19 at 17:23
  • [A byte is 8 bits by definition](https://golang.org/ref/spec#Numeric_types). You can't make a byte with zero bits. You must pick one of the 256 possible values to indicate "empty". – Peter Apr 16 '19 at 17:31
  • 1
    Again, *byte has no length property*. It is a single byte. Its "length" is always exactly one byte. Are you thinking of a byte slice? The Java question you linked has a similar result - you can have an empty string or an empty byte/char array or you can have a byte with the value `0`. – Adrian Apr 16 '19 at 17:31
  • @Adrian thanks! I think that you are right: there is empty string but no empty byte in Go. – dapangmao Apr 16 '19 at 18:15
  • Maybe you were looking for a byte array? That is more like a string. That would be `[]byte("")` – Zan Lynx Apr 16 '19 at 22:37

2 Answers2

2

A byte is an alias to the uint8 type. Having an "empty byte" doesn't really make any sense, just as having an "empty number" doesn't make any sense (you can have the number 0, but what is an "empty" number?)

You can assign a value of zero (b := byte(0), or var b byte), which can be used to indicate that nothing is assigned yet ("zero value"). The byte value of 0 is is known as a "null byte". It normally never occurs in regular text, but often occurs in binary data (e.g. images, compressed files, etc.)

This is different from byte(""), which is a sequence of bytes. You can have a sequence of zero bytes. To give an analogy: I can have a wallet with no money in it, but I can't have a coin that is worth "empty".


If you really want to distinguish between "value of 0" and "never set" you can use either a pointer or a struct. An example with a pointer:

var b *byte
fmt.Println(b)      // <nil>, since it's a pointer which has no address to point to.

one := byte(0)
b = &one            // Set address to one.
fmt.Println(b, *b)  // 0xc000014178 0 (first value will be different, as it's
                    // a memory address).

You'll need to be a little bit careful here, as *b will be a panic if you haven't assigned a value yet. Depending on how it's used it can either work quite well, or be very awkward to work with. An example where this is used in the standard library is the flag package.

Another possibility is to use a struct with separate fiels for the byte itself and a flag to record whether it's been set or not. In the database/sql library there are already the Null* types (e.g. NullInt64, which you can use as a starting point.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
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{}
piecepaper
  • 9
  • 1
  • 2