1

I am trying to translate some C code to Go and I'm wondering if there is a Go equivalent to the following linked list types:

typedef struct TOKENLIST {
  token item;
  struct TOKENLIST *next;
} token_list_elt, *token_list;

So far it appears that I would have to create both types separately like this:

type token_list struct {
    item token
    next *token_list
}
type token_list_elt struct {
    item token
    next *token_list_elt
}

This is not such a big deal for this example, but there are lots linked list types like these I need to translate and some of them have many aliases and/or struct fields.

3
  • Go is not very good at implementing generic data structures. Commented Nov 28, 2017 at 2:42
  • token_list_elt is the struct, and token_list is a pointer to the struct. Is there a reason that you cannot simply translate the code by explicitly instantiating all token_list types as &token_list_elt{ ... } in the go code? Commented Nov 28, 2017 at 3:19
  • This was a bad example, other struct definitions are used for more than just two types where one type is a pointer like I showed here. Commented Nov 28, 2017 at 3:41

1 Answer 1

1

But why not use it like this:

type linkedList struct {
    item token
    next *linkedList
}

...

tokenList := linkedList{}
tokenListElt := linkedList{}

Are different struct types so important?

Sign up to request clarification or add additional context in comments.

1 Comment

I think you are right. Having different types of lists is really not that beneficial. I will just use one type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.