The stack is a plain byte slice from make([]byte, 0, len(s)): its backing array already fits the worst case, so append(stack, ch) never has to grow it, and stack = stack[:len(stack)-1] drops the top without touching memory. The package-level openerFor map goes from each closer to the opener it requires, and the comma-ok lookup opener, isCloser := openerFor[ch] doubles as the closer test. For an opener the key is missing, isCloser comes back false, and the byte is pushed. A closer must find its opener on top; an empty slice or a different byte returns false on the spot. The empty-slice guard sits first in the || on purpose, so stack[len(stack)-1] never runs on an empty slice. Every byte is appended or resliced away at most once, so the walk is O(n), and the slice never holds more than len(s) bytes.