Symbol table datastructure?

(J Ohn Stuart) #1

Is this data structure different than map/hash table ?

Where can I find a Go implementation of it ?

(Johan Dahl) #2

You mean something like this: ?

(Jay Ts) #3

I wrote some symbol table code for a programming language compiler recently, and was surprised at how simple and easy it was in Go. If that’s the kind of symbol table you need, yes, go ahead and use a map. If you need to store different types of data in it, you can do it using an interface{} type, somewhat like this:

type symbol struct {
        name string
        value interface{}

var symbol_table map[string]symbol

// allocate the symbol table (done just once, before using it)
func new_symtab() {
        symbol_table = make(map[string]symbol)

and then add functions for adding and looking up symbols. The last time I did this it was in C, and it took hundreds of lines of code!

The interface{} type works somewhat like a union in C, or “sum type” in some other languages. It allows you to store values of various types in the same variable.