Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5972053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:36:14+00:00 2026-05-22T20:36:14+00:00

I’m learning go, and I would like to explore some patterns. I would like

  • 0

I’m learning go, and I would like to explore some patterns.

I would like to build a Registry component which maintains a map of some stuff, and I want to provide a serialized access to it:

Currently I ended up with something like this:

type JobRegistry struct {
  submission chan JobRegistrySubmitRequest
  listing chan JobRegistryListRequest
}

type JobRegistrySubmitRequest struct {
  request JobSubmissionRequest
  response chan Job
}

type JobRegistryListRequest struct {
  response chan []Job
}

func NewJobRegistry() (this *JobRegistry) {
  this = &JobRegistry{make(chan JobRegistrySubmitRequest, 10), make(chan JobRegistryListRequest, 10)}

  go func() {
    jobMap := make(map[string] Job)

    for {
        select {
        case sub := <- this.submission:
            job := MakeJob(sub.request) // ....

            jobMap[job.Id] = job
            sub.response <- job.Id

        case list := <- this.listing:

            res := make([]Job, 0, 100)
            for _, v := range jobMap {
                res = append(res, v)
            }
            list.response <- res

        }

        /// case somechannel....
     }
   }()

   return
}

Basically, I encapsulate each operation inside a struct, which carries
the parameters and a response channel.

Then I created helper methods for end users:

func (this *JobRegistry) List() ([]Job, os.Error) {
    res := make(chan []Job, 1)
    req := JobRegistryListRequest{res}
    this.listing <- req
    return <-res, nil // todo: handle errors like timeouts
}

I decided to use a channel for each type of request in order to be type safe.


The problem I see with this approach are:

  • A lot of boilerplate code and a lot of places to modify when some param/return type changes

  • Have to do weird things like create yet another wrapper struct in order to return errors from within the handler goroutine. (If I understood correctly there are no tuples, and no way to send multiple values in a channel, like multi-valued returns)

So, I’m wondering whether all this makes sense, or rather just get back to good old locks.

I’m sure that somebody will find some clever way out using channels.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:36:15+00:00Added an answer on May 22, 2026 at 8:36 pm

    I’m not entirely sure I understand you, but I’ll try answering never the less.

    You want a generic service that executes jobs sent to it. You also might want the jobs to be serializable.

    What we need is an interface that would define a generic job.

    type Job interface {
        Run()
        Serialize(io.Writer)
    }
    
    func ReadJob(r io.Reader) {...}
    
    type JobManager struct {
        jobs map[int] Job
        jobs_c chan Job      
    }
    
    func NewJobManager (mgr *JobManager) {
        mgr := &JobManager{make(map[int]Job),make(chan Job,JOB_QUEUE_SIZE)}
        for {
            j,ok := <- jobs_c
            if !ok {break}
            go j.Run()
        }
    }
    
    type IntJob struct{...}
    func (job *IntJob) GetOutChan() chan int {...}
    func (job *IntJob) Run() {...}
    func (job *IntJob) Serialize(o io.Writer) {...}
    

    Much less code, and roughly as useful.

    About signaling errors with an axillary stream, you can always use a helper function.

    type IntChanWithErr struct {
        c chan int
        errc chan os.Error
    }
    func (ch *IntChanWithErr) Next() (v int,err os.Error) {
        select {
            case v := <- ch.c // not handling closed channel
            case err := <- ch.errc
        }
        return
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.