why not heap.Pop return nil while h.Len is 0?

83 views
Skip to first unread message

陈清和

unread,
Oct 11, 2019, 12:14:08 AM10/11/19
to golang-nuts
src/container/heap/heap.go: 

```go
func Pop(h Interface) interface{} {
n := h.Len() - 1
h.Swap(0, n)
down(h, 0, n)
return h.Pop()
}
```

While the h is empty, also the h.Len() equals to 0,why not return nil in advance? In the current way, we have to do boundary check in implement of Swap, otherwise there will be panic.

Ian Lance Taylor

unread,
Oct 11, 2019, 12:30:42 AM10/11/19
to 陈清和, golang-nuts
This is not really different from the fact that

var s []int
fmt.Println(s[0])

will also panic. Yes, if you don't know how many elements you have,
you need to check the length.

Ian

Kurtis Rader

unread,
Oct 11, 2019, 12:41:55 AM10/11/19
to 陈清和, golang-nuts
Note that other languages, such as Python, have the same behavior as Go for the reason mentioned by Ian. If you call Pop() on an empty interface/list/etc., and it returns a distinct value for that condition, you still have to check that the pop failed. It is better to require the caller to do the check before the pop. This means that callers which don't do so will randomly fail. Which is a net positive.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcVjycg_%2BfGJvUzXrq4FARsOz45RTPSi2Fu3HFjDvfc5bg%40mail.gmail.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

陈清和

unread,
Oct 11, 2019, 1:17:36 AM10/11/19
to golang-nuts
well, i know...very appreciate for your explaination, thank you very much.

在 2019年10月11日星期五 UTC+8下午12:41:55,Kurtis Rader写道:
Note that other languages, such as Python, have the same behavior as Go for the reason mentioned by Ian. If you call Pop() on an empty interface/list/etc., and it returns a distinct value for that condition, you still have to check that the pop failed. It is better to require the caller to do the check before the pop. This means that callers which don't do so will randomly fail. Which is a net positive.

On Thu, Oct 10, 2019 at 9:30 PM Ian Lance Taylor <ia...@golang.org> wrote:
On Thu, Oct 10, 2019 at 9:14 PM 陈清和 <chen...@gmail.com> wrote:
>
> src/container/heap/heap.go:
>
> ```go
> func Pop(h Interface) interface{} {
> n := h.Len() - 1
> h.Swap(0, n)
> down(h, 0, n)
> return h.Pop()
> }
> ```
>
> While the h is empty, also the h.Len() equals to 0,why not return nil in advance? In the current way, we have to do boundary check in implement of Swap, otherwise there will be panic.

This is not really different from the fact that

    var s []int
    fmt.Println(s[0])

will also panic.  Yes, if you don't know how many elements you have,
you need to check the length.

Ian

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages