How to obtain a channel “handle” without an associated typeHow to check a channel is closed or not without reading it?How to handle configuration in Gocommunication with channels of type arrayHow are Go channels implemented?How to find a type of an object in Go?Handle netty asynchronous writes and closesHandle different types of messages - one or many channels?Golang Type Switch: How to match a generic slice/array/map/chan?Protobuf, Go and private fieldsGolang: why does this ac variable initialize as chan time.Duration rather than chan chan time.Duration?

Parameterize chained calls to a utility program in Bash

How large would a mega structure have to be to host 1 billion people indefinitely?

Methodology: Writing unit tests for another developer

Why does this method with an optional parameter not override the base class method?

Is it illegal to withhold someone's passport and green card in California?

What does it mean to "control target player"?

Why does the Saturn V have standalone inter-stage rings?

How does the spell Remove Curse interact with a Sword of Vengeance?

What's currently blocking the construction of the wall between Mexico and the US?

What happened to Steve's Shield in Iron Man 2?

Why do textbooks often include the solutions to odd or even numbered problems but not both?

How does a blind passenger not die, if driver becomes unconscious

How does DC work with natural 20?

Helping ease my back pain when I'm studying 13 hours everyday, even weekends

How would modern naval warfare have to have developed differently for battleships to still be relevant in the 21st century?

Why do all the teams that I have worked with always finish a sprint without completion of all the stories?

Non-flat partitions of a set

Is it damaging to turn off a small fridge for two days every week?

Employer wants to use my work email account after I quit

When two first person POV characters meet

Suggested order for Amazon Prime Doctor Who series

Why is it recommended to mix yogurt starter with a small amount of milk before adding to the entire batch?

Is this proposal by U.S. presidential candidate Pete Buttigieg to change the composition of the Supreme Court constitutional?

Dates on degrees don’t make sense – will people care?



How to obtain a channel “handle” without an associated type


How to check a channel is closed or not without reading it?How to handle configuration in Gocommunication with channels of type arrayHow are Go channels implemented?How to find a type of an object in Go?Handle netty asynchronous writes and closesHandle different types of messages - one or many channels?Golang Type Switch: How to match a generic slice/array/map/chan?Protobuf, Go and private fieldsGolang: why does this ac variable initialize as chan time.Duration rather than chan chan time.Duration?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-2















I have a backend (Go server) which services multiple frontends (web pages) and all requests/responses are handled via channels of specific types. Eg, each frontend is associated (on the backend) with a channel where responses are sent (type = chan<- Response).



I have recently implemented a login system where each frontend is associated with a user ID. To keep track of users I have a map:



logins map[chan<- Response]LoginData


Using this I can quickly look up things related to a frontend, such as permissions. This all works fine.



However, to keep things safer and more modular I have moved all the Login stuff to a separate package. This all works except for one gotcha - the logins map is keyed by the type "chan<- Response", but the Response type is defined in my main package and I don't want to expose it to the Login package. (I don't think I could anyway as it would create a circular reference.)



I only want to use the "chan<- Response" as a handle type in the Login package - I don't need to write to that channel from there. I tried converting the channel to an unsafe.Pointer but that is not allowed by the compiler. On the other hand I can't use a pointer to the channel variable (*chan<- Response) as the handle as the channel is stored in several places so the channel variable will have different addresses.



I also tried casting to a different type of chan such as chan int and chan interface but the compiler does not like that. There does not seem to be any way to convert a channel into a "generic" channel.



I really just want the address of the channel's internal data - like you get when you fmt.Printf a channel with %v. The best I can come up with is to use a string like this:



var c chan<- Response = ...
var userID = "steve"
loginKey = fmt.Sprint(c)
Login.Add(loginKey, userID)


I'm not sure this is valid but seems to work, but it seems to me there should be a better way.










share|improve this question
























  • You could use interface as a type

    – Basile Starynkevitch
    Mar 25 at 8:25











  • "the Response type is defined in my main package" - this is a design flaw. The main package should do as little as possible, and it definitely shouldn't define types, for exactly the reason you're seeing here.

    – Adrian
    Mar 25 at 13:39











  • Thanks @Adrian for reading and understanding (and not down-voting what is a reasonable question I think :). I agree absolutely with your comment, but this project started as one main package and grew. I have been trying to separate parts of it into internal packages (as in this question) I will heed your advice and try to work out how to move the types into separate packages.

    – AJR
    Mar 26 at 22:43


















-2















I have a backend (Go server) which services multiple frontends (web pages) and all requests/responses are handled via channels of specific types. Eg, each frontend is associated (on the backend) with a channel where responses are sent (type = chan<- Response).



I have recently implemented a login system where each frontend is associated with a user ID. To keep track of users I have a map:



logins map[chan<- Response]LoginData


Using this I can quickly look up things related to a frontend, such as permissions. This all works fine.



However, to keep things safer and more modular I have moved all the Login stuff to a separate package. This all works except for one gotcha - the logins map is keyed by the type "chan<- Response", but the Response type is defined in my main package and I don't want to expose it to the Login package. (I don't think I could anyway as it would create a circular reference.)



I only want to use the "chan<- Response" as a handle type in the Login package - I don't need to write to that channel from there. I tried converting the channel to an unsafe.Pointer but that is not allowed by the compiler. On the other hand I can't use a pointer to the channel variable (*chan<- Response) as the handle as the channel is stored in several places so the channel variable will have different addresses.



I also tried casting to a different type of chan such as chan int and chan interface but the compiler does not like that. There does not seem to be any way to convert a channel into a "generic" channel.



I really just want the address of the channel's internal data - like you get when you fmt.Printf a channel with %v. The best I can come up with is to use a string like this:



var c chan<- Response = ...
var userID = "steve"
loginKey = fmt.Sprint(c)
Login.Add(loginKey, userID)


I'm not sure this is valid but seems to work, but it seems to me there should be a better way.










share|improve this question
























  • You could use interface as a type

    – Basile Starynkevitch
    Mar 25 at 8:25











  • "the Response type is defined in my main package" - this is a design flaw. The main package should do as little as possible, and it definitely shouldn't define types, for exactly the reason you're seeing here.

    – Adrian
    Mar 25 at 13:39











  • Thanks @Adrian for reading and understanding (and not down-voting what is a reasonable question I think :). I agree absolutely with your comment, but this project started as one main package and grew. I have been trying to separate parts of it into internal packages (as in this question) I will heed your advice and try to work out how to move the types into separate packages.

    – AJR
    Mar 26 at 22:43














-2












-2








-2








I have a backend (Go server) which services multiple frontends (web pages) and all requests/responses are handled via channels of specific types. Eg, each frontend is associated (on the backend) with a channel where responses are sent (type = chan<- Response).



I have recently implemented a login system where each frontend is associated with a user ID. To keep track of users I have a map:



logins map[chan<- Response]LoginData


Using this I can quickly look up things related to a frontend, such as permissions. This all works fine.



However, to keep things safer and more modular I have moved all the Login stuff to a separate package. This all works except for one gotcha - the logins map is keyed by the type "chan<- Response", but the Response type is defined in my main package and I don't want to expose it to the Login package. (I don't think I could anyway as it would create a circular reference.)



I only want to use the "chan<- Response" as a handle type in the Login package - I don't need to write to that channel from there. I tried converting the channel to an unsafe.Pointer but that is not allowed by the compiler. On the other hand I can't use a pointer to the channel variable (*chan<- Response) as the handle as the channel is stored in several places so the channel variable will have different addresses.



I also tried casting to a different type of chan such as chan int and chan interface but the compiler does not like that. There does not seem to be any way to convert a channel into a "generic" channel.



I really just want the address of the channel's internal data - like you get when you fmt.Printf a channel with %v. The best I can come up with is to use a string like this:



var c chan<- Response = ...
var userID = "steve"
loginKey = fmt.Sprint(c)
Login.Add(loginKey, userID)


I'm not sure this is valid but seems to work, but it seems to me there should be a better way.










share|improve this question
















I have a backend (Go server) which services multiple frontends (web pages) and all requests/responses are handled via channels of specific types. Eg, each frontend is associated (on the backend) with a channel where responses are sent (type = chan<- Response).



I have recently implemented a login system where each frontend is associated with a user ID. To keep track of users I have a map:



logins map[chan<- Response]LoginData


Using this I can quickly look up things related to a frontend, such as permissions. This all works fine.



However, to keep things safer and more modular I have moved all the Login stuff to a separate package. This all works except for one gotcha - the logins map is keyed by the type "chan<- Response", but the Response type is defined in my main package and I don't want to expose it to the Login package. (I don't think I could anyway as it would create a circular reference.)



I only want to use the "chan<- Response" as a handle type in the Login package - I don't need to write to that channel from there. I tried converting the channel to an unsafe.Pointer but that is not allowed by the compiler. On the other hand I can't use a pointer to the channel variable (*chan<- Response) as the handle as the channel is stored in several places so the channel variable will have different addresses.



I also tried casting to a different type of chan such as chan int and chan interface but the compiler does not like that. There does not seem to be any way to convert a channel into a "generic" channel.



I really just want the address of the channel's internal data - like you get when you fmt.Printf a channel with %v. The best I can come up with is to use a string like this:



var c chan<- Response = ...
var userID = "steve"
loginKey = fmt.Sprint(c)
Login.Add(loginKey, userID)


I'm not sure this is valid but seems to work, but it seems to me there should be a better way.







go channel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 8:28







AJR

















asked Mar 25 at 8:23









AJRAJR

736




736












  • You could use interface as a type

    – Basile Starynkevitch
    Mar 25 at 8:25











  • "the Response type is defined in my main package" - this is a design flaw. The main package should do as little as possible, and it definitely shouldn't define types, for exactly the reason you're seeing here.

    – Adrian
    Mar 25 at 13:39











  • Thanks @Adrian for reading and understanding (and not down-voting what is a reasonable question I think :). I agree absolutely with your comment, but this project started as one main package and grew. I have been trying to separate parts of it into internal packages (as in this question) I will heed your advice and try to work out how to move the types into separate packages.

    – AJR
    Mar 26 at 22:43


















  • You could use interface as a type

    – Basile Starynkevitch
    Mar 25 at 8:25











  • "the Response type is defined in my main package" - this is a design flaw. The main package should do as little as possible, and it definitely shouldn't define types, for exactly the reason you're seeing here.

    – Adrian
    Mar 25 at 13:39











  • Thanks @Adrian for reading and understanding (and not down-voting what is a reasonable question I think :). I agree absolutely with your comment, but this project started as one main package and grew. I have been trying to separate parts of it into internal packages (as in this question) I will heed your advice and try to work out how to move the types into separate packages.

    – AJR
    Mar 26 at 22:43

















You could use interface as a type

– Basile Starynkevitch
Mar 25 at 8:25





You could use interface as a type

– Basile Starynkevitch
Mar 25 at 8:25













"the Response type is defined in my main package" - this is a design flaw. The main package should do as little as possible, and it definitely shouldn't define types, for exactly the reason you're seeing here.

– Adrian
Mar 25 at 13:39





"the Response type is defined in my main package" - this is a design flaw. The main package should do as little as possible, and it definitely shouldn't define types, for exactly the reason you're seeing here.

– Adrian
Mar 25 at 13:39













Thanks @Adrian for reading and understanding (and not down-voting what is a reasonable question I think :). I agree absolutely with your comment, but this project started as one main package and grew. I have been trying to separate parts of it into internal packages (as in this question) I will heed your advice and try to work out how to move the types into separate packages.

– AJR
Mar 26 at 22:43






Thanks @Adrian for reading and understanding (and not down-voting what is a reasonable question I think :). I agree absolutely with your comment, but this project started as one main package and grew. I have been trying to separate parts of it into internal packages (as in this question) I will heed your advice and try to work out how to move the types into separate packages.

– AJR
Mar 26 at 22:43













1 Answer
1






active

oldest

votes


















0















On the other hand I can't use a pointer to the channel variable (*chan<- Response) as the handle as the channel is stored in several places so the channel variable will have different addresses.




But this is the only (halfway acceptable solution): Do not pass around a chan Response but a *(chan Response) (adding directions to your likings). Everything else is crap. Best thing to do is hiding this chan Response in a type.






share|improve this answer























  • Thanks. You are right it should be easy enough to have one chan variable and pass around a pointer to it. It just seems a bit redundant as I think a channel variable is essentially just a pointer to the real channel. I agree the best thing is to create a new type but it makes little sense to declare it in my Login package and I can't declare it in the main package (circular ref), so I guess I need to put it in a new package that I import into main and Login packages.

    – AJR
    Mar 26 at 22:33














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%2f55333707%2fhow-to-obtain-a-channel-handle-without-an-associated-type%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0















On the other hand I can't use a pointer to the channel variable (*chan<- Response) as the handle as the channel is stored in several places so the channel variable will have different addresses.




But this is the only (halfway acceptable solution): Do not pass around a chan Response but a *(chan Response) (adding directions to your likings). Everything else is crap. Best thing to do is hiding this chan Response in a type.






share|improve this answer























  • Thanks. You are right it should be easy enough to have one chan variable and pass around a pointer to it. It just seems a bit redundant as I think a channel variable is essentially just a pointer to the real channel. I agree the best thing is to create a new type but it makes little sense to declare it in my Login package and I can't declare it in the main package (circular ref), so I guess I need to put it in a new package that I import into main and Login packages.

    – AJR
    Mar 26 at 22:33
















0















On the other hand I can't use a pointer to the channel variable (*chan<- Response) as the handle as the channel is stored in several places so the channel variable will have different addresses.




But this is the only (halfway acceptable solution): Do not pass around a chan Response but a *(chan Response) (adding directions to your likings). Everything else is crap. Best thing to do is hiding this chan Response in a type.






share|improve this answer























  • Thanks. You are right it should be easy enough to have one chan variable and pass around a pointer to it. It just seems a bit redundant as I think a channel variable is essentially just a pointer to the real channel. I agree the best thing is to create a new type but it makes little sense to declare it in my Login package and I can't declare it in the main package (circular ref), so I guess I need to put it in a new package that I import into main and Login packages.

    – AJR
    Mar 26 at 22:33














0












0








0








On the other hand I can't use a pointer to the channel variable (*chan<- Response) as the handle as the channel is stored in several places so the channel variable will have different addresses.




But this is the only (halfway acceptable solution): Do not pass around a chan Response but a *(chan Response) (adding directions to your likings). Everything else is crap. Best thing to do is hiding this chan Response in a type.






share|improve this answer














On the other hand I can't use a pointer to the channel variable (*chan<- Response) as the handle as the channel is stored in several places so the channel variable will have different addresses.




But this is the only (halfway acceptable solution): Do not pass around a chan Response but a *(chan Response) (adding directions to your likings). Everything else is crap. Best thing to do is hiding this chan Response in a type.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 25 at 8:52









VolkerVolker

22.2k34960




22.2k34960












  • Thanks. You are right it should be easy enough to have one chan variable and pass around a pointer to it. It just seems a bit redundant as I think a channel variable is essentially just a pointer to the real channel. I agree the best thing is to create a new type but it makes little sense to declare it in my Login package and I can't declare it in the main package (circular ref), so I guess I need to put it in a new package that I import into main and Login packages.

    – AJR
    Mar 26 at 22:33


















  • Thanks. You are right it should be easy enough to have one chan variable and pass around a pointer to it. It just seems a bit redundant as I think a channel variable is essentially just a pointer to the real channel. I agree the best thing is to create a new type but it makes little sense to declare it in my Login package and I can't declare it in the main package (circular ref), so I guess I need to put it in a new package that I import into main and Login packages.

    – AJR
    Mar 26 at 22:33

















Thanks. You are right it should be easy enough to have one chan variable and pass around a pointer to it. It just seems a bit redundant as I think a channel variable is essentially just a pointer to the real channel. I agree the best thing is to create a new type but it makes little sense to declare it in my Login package and I can't declare it in the main package (circular ref), so I guess I need to put it in a new package that I import into main and Login packages.

– AJR
Mar 26 at 22:33






Thanks. You are right it should be easy enough to have one chan variable and pass around a pointer to it. It just seems a bit redundant as I think a channel variable is essentially just a pointer to the real channel. I agree the best thing is to create a new type but it makes little sense to declare it in my Login package and I can't declare it in the main package (circular ref), so I guess I need to put it in a new package that I import into main and Login packages.

– AJR
Mar 26 at 22:33




















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%2f55333707%2fhow-to-obtain-a-channel-handle-without-an-associated-type%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

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해