Skip to content

get non-existence key from hash would cause panic #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
qianguozheng opened this issue Jan 18, 2017 · 3 comments
Closed

get non-existence key from hash would cause panic #3

qianguozheng opened this issue Jan 18, 2017 · 3 comments

Comments

@qianguozheng
Copy link

package main

import (
	hashtable "github.com/timtadh/data-structures/hashtable"
	"fmt"
	types "github.com/timtadh/data-structures/types"
)

func main()  {
	hash := hashtable.NewHashTable(10)
	
	hash.Put(types.String(1),"a")
	v , err :=hash.Get(types.String(1))
	if err != nil{
		fmt.Println(err.Error())
	}else{
		if v != nil{
			fmt.Println(v.(string))
		}
	}
}
@timtadh
Copy link
Owner

timtadh commented Jan 23, 2017

Hi! Sorry for the super slow response (I was out of town this past week). I will take a look at this ASAP. You may in the mean time want to use the Linear Hash Table which has better performance.

@timtadh
Copy link
Owner

timtadh commented Jan 23, 2017

Hi, it doesn't look like a panic occurs to me. The stack trace is printed out because all errors from this library should have a stack trace attached to them. Let me know if you have any questions.

@timtadh
Copy link
Owner

timtadh commented Jan 23, 2017

Closing this issue. This sample code makes it more obvious:

package main

import (
	"fmt"

	hashtable "github.com/timtadh/data-structures/hashtable"
	types "github.com/timtadh/data-structures/types"
)

func main() {
	hash := hashtable.NewHashTable(10)
	hash.Put(types.Int(1), "a")
	v, err := hash.Get(types.Int(2))
	if err != nil {
		fmt.Println("this is an error not a panic\n", err)
	} else {
		if v != nil {
			fmt.Println(v.(string))
		}
	}
	fmt.Println("done")
}

@timtadh timtadh closed this as completed Jan 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants