Creating a struct that contains list types in go












-2















I have created a struct, this struct has contains two list types within it. When I try to instantiate my struct I receive the error



cannot use list.New() (type *list.List) as type list.List in field value



I am using the golang playground



The struct



type myStruct struct {
name string
messages list.List
users list.List
lastUsed time.Time
}


Instantiating the struct



var myVar = myStruct{"hello", list.New(), list.New(), time.Now()}









share|improve this question























  • The error message is pretty clear: You're trying to assign a pointer to a concrete value.

    – Flimzy
    Jan 2 at 9:17











  • @Flimzy The error message doesn't mention a pointer or a concrete value, so it wasn't clear to me. I understand now that the * means pointer, but it's been a few years since I've worked with pointers.

    – Jacob
    Jan 2 at 9:19








  • 1





    Well, it does mention those. *list.List means pointer, list.List means concrete value. If these concepts are new to you, you should start with A Tour of Go.

    – Flimzy
    Jan 2 at 9:21











  • @Flimzy They are not new, I am just rusty using them, I've already done that tour.

    – Jacob
    Jan 2 at 9:23
















-2















I have created a struct, this struct has contains two list types within it. When I try to instantiate my struct I receive the error



cannot use list.New() (type *list.List) as type list.List in field value



I am using the golang playground



The struct



type myStruct struct {
name string
messages list.List
users list.List
lastUsed time.Time
}


Instantiating the struct



var myVar = myStruct{"hello", list.New(), list.New(), time.Now()}









share|improve this question























  • The error message is pretty clear: You're trying to assign a pointer to a concrete value.

    – Flimzy
    Jan 2 at 9:17











  • @Flimzy The error message doesn't mention a pointer or a concrete value, so it wasn't clear to me. I understand now that the * means pointer, but it's been a few years since I've worked with pointers.

    – Jacob
    Jan 2 at 9:19








  • 1





    Well, it does mention those. *list.List means pointer, list.List means concrete value. If these concepts are new to you, you should start with A Tour of Go.

    – Flimzy
    Jan 2 at 9:21











  • @Flimzy They are not new, I am just rusty using them, I've already done that tour.

    – Jacob
    Jan 2 at 9:23














-2












-2








-2








I have created a struct, this struct has contains two list types within it. When I try to instantiate my struct I receive the error



cannot use list.New() (type *list.List) as type list.List in field value



I am using the golang playground



The struct



type myStruct struct {
name string
messages list.List
users list.List
lastUsed time.Time
}


Instantiating the struct



var myVar = myStruct{"hello", list.New(), list.New(), time.Now()}









share|improve this question














I have created a struct, this struct has contains two list types within it. When I try to instantiate my struct I receive the error



cannot use list.New() (type *list.List) as type list.List in field value



I am using the golang playground



The struct



type myStruct struct {
name string
messages list.List
users list.List
lastUsed time.Time
}


Instantiating the struct



var myVar = myStruct{"hello", list.New(), list.New(), time.Now()}






list go struct






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 8:50









JacobJacob

70324




70324













  • The error message is pretty clear: You're trying to assign a pointer to a concrete value.

    – Flimzy
    Jan 2 at 9:17











  • @Flimzy The error message doesn't mention a pointer or a concrete value, so it wasn't clear to me. I understand now that the * means pointer, but it's been a few years since I've worked with pointers.

    – Jacob
    Jan 2 at 9:19








  • 1





    Well, it does mention those. *list.List means pointer, list.List means concrete value. If these concepts are new to you, you should start with A Tour of Go.

    – Flimzy
    Jan 2 at 9:21











  • @Flimzy They are not new, I am just rusty using them, I've already done that tour.

    – Jacob
    Jan 2 at 9:23



















  • The error message is pretty clear: You're trying to assign a pointer to a concrete value.

    – Flimzy
    Jan 2 at 9:17











  • @Flimzy The error message doesn't mention a pointer or a concrete value, so it wasn't clear to me. I understand now that the * means pointer, but it's been a few years since I've worked with pointers.

    – Jacob
    Jan 2 at 9:19








  • 1





    Well, it does mention those. *list.List means pointer, list.List means concrete value. If these concepts are new to you, you should start with A Tour of Go.

    – Flimzy
    Jan 2 at 9:21











  • @Flimzy They are not new, I am just rusty using them, I've already done that tour.

    – Jacob
    Jan 2 at 9:23

















The error message is pretty clear: You're trying to assign a pointer to a concrete value.

– Flimzy
Jan 2 at 9:17





The error message is pretty clear: You're trying to assign a pointer to a concrete value.

– Flimzy
Jan 2 at 9:17













@Flimzy The error message doesn't mention a pointer or a concrete value, so it wasn't clear to me. I understand now that the * means pointer, but it's been a few years since I've worked with pointers.

– Jacob
Jan 2 at 9:19







@Flimzy The error message doesn't mention a pointer or a concrete value, so it wasn't clear to me. I understand now that the * means pointer, but it's been a few years since I've worked with pointers.

– Jacob
Jan 2 at 9:19






1




1





Well, it does mention those. *list.List means pointer, list.List means concrete value. If these concepts are new to you, you should start with A Tour of Go.

– Flimzy
Jan 2 at 9:21





Well, it does mention those. *list.List means pointer, list.List means concrete value. If these concepts are new to you, you should start with A Tour of Go.

– Flimzy
Jan 2 at 9:21













@Flimzy They are not new, I am just rusty using them, I've already done that tour.

– Jacob
Jan 2 at 9:23





@Flimzy They are not new, I am just rusty using them, I've already done that tour.

– Jacob
Jan 2 at 9:23












2 Answers
2






active

oldest

votes


















3














list.New() returns a pointer *List, while myStruct announce its fields as List.




func New() *List




messages and users should be *list.List



type myStruct struct {
name string
messages *list.List
users *list.List
lastUsed time.Time
}


Another approach according to your need, u can just initialize the struct as below:



var myVar = myStruct{"hello", *list.New(), *list.New(), time.Now()}





share|improve this answer


























  • Thanks, will using pointers to lists cause any problems? My original intention was to just use a list and not use a pointer. Is there a way to pass a new list to the struct instead of passing a pointer?

    – Jacob
    Jan 2 at 9:15






  • 1





    @Jacob edited answer.

    – Bob Fred
    Jan 2 at 9:26











  • So now your dereferencing the list.New() when you create it, so that you pass the actual list into the struct, is that correct?

    – Jacob
    Jan 2 at 10:41













  • You have to remove the '*' from the struct if you add it in the function call. I'm not sure which one I should be using. The struct is going to be a chatroom, that holds a list of messages and a list of users. I'm adding these chatrooms to a global list in my program. Would I want the struct to have pointers to lists, or concrete lists.

    – Jacob
    Jan 2 at 10:50











  • @Jacob see pointers vs values

    – Bob Fred
    Jan 2 at 11:37



















2














You are creating wrong struct because go, according to list package New() method returns pointer type of list and you created list in struct without pointer.



func New() *List


So, according to doc you need to created your struct like below:



type myStruct struct {
name string
messages *list.List
users *list.List
lastUsed time.Time
}


Go Playground






share|improve this answer
























  • Thanks, will using pointers to lists cause any problems? My original intention was to just use a list and not use a pointer. Is there a way to pass a new list to the struct instead of passing a pointer?

    – Jacob
    Jan 2 at 9:16











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003473%2fcreating-a-struct-that-contains-list-types-in-go%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














list.New() returns a pointer *List, while myStruct announce its fields as List.




func New() *List




messages and users should be *list.List



type myStruct struct {
name string
messages *list.List
users *list.List
lastUsed time.Time
}


Another approach according to your need, u can just initialize the struct as below:



var myVar = myStruct{"hello", *list.New(), *list.New(), time.Now()}





share|improve this answer


























  • Thanks, will using pointers to lists cause any problems? My original intention was to just use a list and not use a pointer. Is there a way to pass a new list to the struct instead of passing a pointer?

    – Jacob
    Jan 2 at 9:15






  • 1





    @Jacob edited answer.

    – Bob Fred
    Jan 2 at 9:26











  • So now your dereferencing the list.New() when you create it, so that you pass the actual list into the struct, is that correct?

    – Jacob
    Jan 2 at 10:41













  • You have to remove the '*' from the struct if you add it in the function call. I'm not sure which one I should be using. The struct is going to be a chatroom, that holds a list of messages and a list of users. I'm adding these chatrooms to a global list in my program. Would I want the struct to have pointers to lists, or concrete lists.

    – Jacob
    Jan 2 at 10:50











  • @Jacob see pointers vs values

    – Bob Fred
    Jan 2 at 11:37
















3














list.New() returns a pointer *List, while myStruct announce its fields as List.




func New() *List




messages and users should be *list.List



type myStruct struct {
name string
messages *list.List
users *list.List
lastUsed time.Time
}


Another approach according to your need, u can just initialize the struct as below:



var myVar = myStruct{"hello", *list.New(), *list.New(), time.Now()}





share|improve this answer


























  • Thanks, will using pointers to lists cause any problems? My original intention was to just use a list and not use a pointer. Is there a way to pass a new list to the struct instead of passing a pointer?

    – Jacob
    Jan 2 at 9:15






  • 1





    @Jacob edited answer.

    – Bob Fred
    Jan 2 at 9:26











  • So now your dereferencing the list.New() when you create it, so that you pass the actual list into the struct, is that correct?

    – Jacob
    Jan 2 at 10:41













  • You have to remove the '*' from the struct if you add it in the function call. I'm not sure which one I should be using. The struct is going to be a chatroom, that holds a list of messages and a list of users. I'm adding these chatrooms to a global list in my program. Would I want the struct to have pointers to lists, or concrete lists.

    – Jacob
    Jan 2 at 10:50











  • @Jacob see pointers vs values

    – Bob Fred
    Jan 2 at 11:37














3












3








3







list.New() returns a pointer *List, while myStruct announce its fields as List.




func New() *List




messages and users should be *list.List



type myStruct struct {
name string
messages *list.List
users *list.List
lastUsed time.Time
}


Another approach according to your need, u can just initialize the struct as below:



var myVar = myStruct{"hello", *list.New(), *list.New(), time.Now()}





share|improve this answer















list.New() returns a pointer *List, while myStruct announce its fields as List.




func New() *List




messages and users should be *list.List



type myStruct struct {
name string
messages *list.List
users *list.List
lastUsed time.Time
}


Another approach according to your need, u can just initialize the struct as below:



var myVar = myStruct{"hello", *list.New(), *list.New(), time.Now()}






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 9:20

























answered Jan 2 at 9:08









Bob FredBob Fred

2015




2015













  • Thanks, will using pointers to lists cause any problems? My original intention was to just use a list and not use a pointer. Is there a way to pass a new list to the struct instead of passing a pointer?

    – Jacob
    Jan 2 at 9:15






  • 1





    @Jacob edited answer.

    – Bob Fred
    Jan 2 at 9:26











  • So now your dereferencing the list.New() when you create it, so that you pass the actual list into the struct, is that correct?

    – Jacob
    Jan 2 at 10:41













  • You have to remove the '*' from the struct if you add it in the function call. I'm not sure which one I should be using. The struct is going to be a chatroom, that holds a list of messages and a list of users. I'm adding these chatrooms to a global list in my program. Would I want the struct to have pointers to lists, or concrete lists.

    – Jacob
    Jan 2 at 10:50











  • @Jacob see pointers vs values

    – Bob Fred
    Jan 2 at 11:37



















  • Thanks, will using pointers to lists cause any problems? My original intention was to just use a list and not use a pointer. Is there a way to pass a new list to the struct instead of passing a pointer?

    – Jacob
    Jan 2 at 9:15






  • 1





    @Jacob edited answer.

    – Bob Fred
    Jan 2 at 9:26











  • So now your dereferencing the list.New() when you create it, so that you pass the actual list into the struct, is that correct?

    – Jacob
    Jan 2 at 10:41













  • You have to remove the '*' from the struct if you add it in the function call. I'm not sure which one I should be using. The struct is going to be a chatroom, that holds a list of messages and a list of users. I'm adding these chatrooms to a global list in my program. Would I want the struct to have pointers to lists, or concrete lists.

    – Jacob
    Jan 2 at 10:50











  • @Jacob see pointers vs values

    – Bob Fred
    Jan 2 at 11:37

















Thanks, will using pointers to lists cause any problems? My original intention was to just use a list and not use a pointer. Is there a way to pass a new list to the struct instead of passing a pointer?

– Jacob
Jan 2 at 9:15





Thanks, will using pointers to lists cause any problems? My original intention was to just use a list and not use a pointer. Is there a way to pass a new list to the struct instead of passing a pointer?

– Jacob
Jan 2 at 9:15




1




1





@Jacob edited answer.

– Bob Fred
Jan 2 at 9:26





@Jacob edited answer.

– Bob Fred
Jan 2 at 9:26













So now your dereferencing the list.New() when you create it, so that you pass the actual list into the struct, is that correct?

– Jacob
Jan 2 at 10:41







So now your dereferencing the list.New() when you create it, so that you pass the actual list into the struct, is that correct?

– Jacob
Jan 2 at 10:41















You have to remove the '*' from the struct if you add it in the function call. I'm not sure which one I should be using. The struct is going to be a chatroom, that holds a list of messages and a list of users. I'm adding these chatrooms to a global list in my program. Would I want the struct to have pointers to lists, or concrete lists.

– Jacob
Jan 2 at 10:50





You have to remove the '*' from the struct if you add it in the function call. I'm not sure which one I should be using. The struct is going to be a chatroom, that holds a list of messages and a list of users. I'm adding these chatrooms to a global list in my program. Would I want the struct to have pointers to lists, or concrete lists.

– Jacob
Jan 2 at 10:50













@Jacob see pointers vs values

– Bob Fred
Jan 2 at 11:37





@Jacob see pointers vs values

– Bob Fred
Jan 2 at 11:37













2














You are creating wrong struct because go, according to list package New() method returns pointer type of list and you created list in struct without pointer.



func New() *List


So, according to doc you need to created your struct like below:



type myStruct struct {
name string
messages *list.List
users *list.List
lastUsed time.Time
}


Go Playground






share|improve this answer
























  • Thanks, will using pointers to lists cause any problems? My original intention was to just use a list and not use a pointer. Is there a way to pass a new list to the struct instead of passing a pointer?

    – Jacob
    Jan 2 at 9:16
















2














You are creating wrong struct because go, according to list package New() method returns pointer type of list and you created list in struct without pointer.



func New() *List


So, according to doc you need to created your struct like below:



type myStruct struct {
name string
messages *list.List
users *list.List
lastUsed time.Time
}


Go Playground






share|improve this answer
























  • Thanks, will using pointers to lists cause any problems? My original intention was to just use a list and not use a pointer. Is there a way to pass a new list to the struct instead of passing a pointer?

    – Jacob
    Jan 2 at 9:16














2












2








2







You are creating wrong struct because go, according to list package New() method returns pointer type of list and you created list in struct without pointer.



func New() *List


So, according to doc you need to created your struct like below:



type myStruct struct {
name string
messages *list.List
users *list.List
lastUsed time.Time
}


Go Playground






share|improve this answer













You are creating wrong struct because go, according to list package New() method returns pointer type of list and you created list in struct without pointer.



func New() *List


So, according to doc you need to created your struct like below:



type myStruct struct {
name string
messages *list.List
users *list.List
lastUsed time.Time
}


Go Playground







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 9:11









saddamsaddam

693521




693521













  • Thanks, will using pointers to lists cause any problems? My original intention was to just use a list and not use a pointer. Is there a way to pass a new list to the struct instead of passing a pointer?

    – Jacob
    Jan 2 at 9:16



















  • Thanks, will using pointers to lists cause any problems? My original intention was to just use a list and not use a pointer. Is there a way to pass a new list to the struct instead of passing a pointer?

    – Jacob
    Jan 2 at 9:16

















Thanks, will using pointers to lists cause any problems? My original intention was to just use a list and not use a pointer. Is there a way to pass a new list to the struct instead of passing a pointer?

– Jacob
Jan 2 at 9:16





Thanks, will using pointers to lists cause any problems? My original intention was to just use a list and not use a pointer. Is there a way to pass a new list to the struct instead of passing a pointer?

– Jacob
Jan 2 at 9:16


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003473%2fcreating-a-struct-that-contains-list-types-in-go%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas