can i get goroutine`s id or have some local storage of a goroutine?

988 views
Skip to first unread message

韩拓

unread,
Feb 15, 2012, 3:32:45 AM2/15/12
to golang-nuts
hi list,
my problem is, i have a typical http server,like:
func A(w http.ResponseWriter, req *http.Request) {
  B()
  C()
}

func B() {
  // ...
}
func C() {
  // ...
}

now i want to log something (per request),like:
func A(w http.ResponseWriter, req *http.Request) {
  log := NewLog()
  log.Info("call B")
  B()
  log.Info("call C")
  C()
  if (apiDebug) { ResponseLog(w, log) }
}

func B() {
  // ...
}
func C() {
  // ...
}

the problem is i can`t log in func B or func C,unless modify their signature :
func B(log Log) or func B(cxt Context)
but there are lots of codes,nearly all my functions need modify.

so, if i can get the current goroutine`s id or its local storage(if have),i can do this:

func A(w http.ResponseWriter, req *http.Request) {
  SetContext(xxx) // or SetContext(xxx, myGoroutineId)
  B()
  C()
  ClearContext(myGoroutineId)
}

func B() {
  log := GetContext() // or GetContext(myGoroutineId)
  log.Info("im in func B!")
  // ...
}
func C() {
  // ...
}

could i do this in golang?

thanks.

--
    此致,
敬礼!

                     韩拓

David Symonds

unread,
Feb 15, 2012, 3:36:23 AM2/15/12
to 韩拓, golang-nuts
On Wed, Feb 15, 2012 at 7:32 PM, 韩拓 <hantu...@gmail.com> wrote:

> could i do this in golang?

No. Go does not have any form of goroutine-local or thread-local
storage, nor identifiers.


Dave.

Jessta

unread,
Feb 15, 2012, 3:47:42 AM2/15/12
to 韩拓, golang-nuts
2012/2/15 韩拓 <hantu...@gmail.com>:

> so, if i can get the current goroutine`s id or its local storage(if have),i
> can do this:

goroutines don't have any kind of local storage or id.

If you want to pass the same state of a number of functions then you
should make them methods on a struct that stores that state.

eg.
type Context struct {
logger log.Logger


}
func A(w http.ResponseWriter, req *http.Request) {

  a:= new(Context)
a.logger = log.New()
a.B()
  a.C()
}

func (c *Context)B() {
  c.logger.Info("im in func B!")

  // ...
}
func (c *Context) C() {
  // ...
}


--
=====================
http://jessta.id.au

navindr...@gmail.com

unread,
May 19, 2017, 10:52:42 AM5/19/17
to golang-nuts, hantu...@gmail.com
Hi, 
   I just read this content as I was looking for same thing, I tried as you suggested but was unable to do so, as I am using function calls from different packages.
My request flow is some what like this:
 

package global

type Context struct {
     logger log.Logger
}

   
    package controller

 func A(w http.ResponseWriter, req *http.Request) {
  

   ABC.B()
   XYZ.C()
  }

    
    package ABC 

 func B() {
   QWE.D()

   // ...
 }


    package XYZ 

 func C() {

   // ...
 }



   package QWE
   func D(){
     //....
   }

I tried to define function B,C,D on global Context structure but was giving error. Please suggest how to resolve this.

will....@bigcommerce.com

unread,
May 20, 2017, 11:01:44 AM5/20/17
to golang-nuts
Can you wrap them in a struct and call them through a method?

xjdrew

unread,
May 21, 2017, 3:14:42 AM5/21/17
to golang-nuts, hantu...@gmail.com
Do you call B and C with any arguments? 

if you have call B and C with req, you can use the method: req.WithContext as below:

func A(w http.ResponseWriter, req *http.Request) {
  ctx := Context.WithValue(req.Context(), "log", log)
  req := req.WithContext(ctx)
  B(req)
  C(req)
}


func B(req) {
  log := req.Value("log"
  log.Info("im in func B!")
  // ...
}

Reply all
Reply to author
Forward
0 new messages