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 8630217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:00:56+00:00 2026-06-12T09:00:56+00:00

Ok i have a main package and a http handler package. Essentially what i

  • 0

Ok i have a main package and a http handler package. Essentially what i am trying to do is setup a global struct so that way i can call upon information in that struct at any time.

Basic outline of my attempted example below:
Main package imports handler function
Main package calls handlerfunc
Handlerfunc sets http.ResponseWriter and other items into UrlInfo struct
Handlerfunc runs passed in function (without having to pass UrlStruct into function)
Run function (home in this example)
Function home can call upon variable uinfo at any time cause its a pointer UrlInfo struct

Obviously this doesnt work, but this is essentially what i would like to do so that way im not having to pass all this info into my home function. Keeping it clean and simple.

Any thoughts and ideas are welcome. Thanks.

Handler Package

package handler

// Struct containing http requests and variables
type UrlInfo struct {
    Res http.ResponseWriter
    Req *http.Request
    Item string
}

func HandleFunc(handlepath string, runfunc func()) {
    // Set handler and setup struct
    http.HandleFunc(handlepath, func(w http.ResponseWriter, r *http.Request) {
        url := new(UrlInfo)
        url.Res = w
        url.Req = r
        url.Item = "Item information"

        runfunc()
    })
}

Main Package

import "handler"

var uinfo = &handler.UrlInfo{}

func init() {
    handler.HandleFunc("/home/", home)
}

func home() {
    fmt.Println(uinfo.Item)
}
  • 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-06-12T09:00:57+00:00Added an answer on June 12, 2026 at 9:00 am

    From what I gather from your question, you are attempting to define a single, global instance of a structure which, among other things, holds a reference to the current Request and ResponseWriter.

    If this is the intention, I should warn you this is going to cause problems.
    Go’s http package executes each request handler in a separate goroutine. This means that you can have arbitrarily many requests being handled simultaneously. Therefore they can not all refer to the same global structure safely and expect it to contain request information relevant only to that particular request. The global instance should not be used if you expect your server to be thread safe.

    Keeping the code clean by grouping extraneous parameters in a structure can be handy, but in your case, I do not believe you can (or should) avoid passing a new instance of UrlInfo structure directly to home() as a parameter. It will make things unnecessarily complex and unpredictable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example.go, I have package main import html/template import net/http func handler(w http.ResponseWriter, r
I'm trying to set cookies with Go's net/http package. I have: package main import
I have the following global in variable in file_1.pl, in package main: package main;
I have 2 classes within same package. Both classes have main method in them.
Say i have this this structure. MyApp ├── main.py └── package ├── __init__.py ├──
I have main table called 'Employee' and another slave table called 'EmployeeTypes' that has
Splash I have main activity UI startup operations that take between 5-10 seconds (that
I have a main-section div that is set to inherit it's height from its'
solved by Emil thx I have following in my main package com.example.surfacetest; import android.os.Bundle;
I have the following situation: I have an event handler, that displays small messages

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.