GithubHelp home page GithubHelp logo

Comments (5)

crackcomm avatar crackcomm commented on August 17, 2024

(example)

type Handler struct {
    test string
}
server.Handle(
    server.NewHandler(
        &Handler{test: "initialized"},
    ),
)

from go-micro.

jbrodriguez avatar jbrodriguez commented on August 17, 2024

Thank you @crackcomm , but what if I need to run some code to initialize the variables (rather than a static initialization as in the example) ?

from go-micro.

crackcomm avatar crackcomm commented on August 17, 2024

Then use a function.

package handler

type Handler struct {
    // ...
}

// New - Constructs a new Handler.
func New(opts interface{}) *Handler {
    // do the stuff (dialing database probably does not belong here)
    return &Handler{}
}
server.Handle(
    server.NewHandler(handler.New("initialized")),
)

from go-micro.

asim avatar asim commented on August 17, 2024

Łukasz's above example works fine for this. Alternatively you can use the sync package Once type https://golang.org/pkg/sync/#Once.Do. There's no clean one time startup initialisation as of yet. It was purposely kept out to keep things simple at first and see where the use cases came in for other things.

I think we'll potentially add the notion of before/after functions for start, stop and handler requests.

from go-micro.

jbrodriguez avatar jbrodriguez commented on August 17, 2024

ok, thanks @crackcomm and @asim !

from go-micro.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.