Connection Scoped Variables - best practice?

This is really going to show my Go ignorance as I am sure it a very basic question for most - I’m just getting started.

In PHP when someone connects a Global variable isn’t truly global because it’s automatically restricted to the session/connection so as soon as that request is processed the “kind of” global variable is automatically destroyed. Also, any other connections that come in at the same time are completely isolated so there can be multiple independent occurrences of that Global variable. Like with any programming, in PHP the better programmers rarely use global variables but they do have their uses. A User variable for that particular connection is one such case since it is so commonly used it doesn’t make sense explicitly to pass it to all the routines that use it.

If I am understanding Go correctly a global variable is truly global so even between connections the same global variable would be seen by all connections. This means you can’t just set a global user because the next connection may set it to something different and you have conflicts. Also, it won’t automatically get destroyed at the end of the connection so that will need to be done explicitly.

So my questions are two-fold:

  • How close is my assessment of the differences and what as I missing in understanding?
  • What is the best practice for a “per connection” variable such as the user associated with that connection?

I would argue that there is no ‘connection’, even in PHP. While HTTP technically uses connections, every request/response pair stands on its own. There can be data stored in a session but this data must be retrieved for every request that presents the session ID.

Do you ask about sessions? These are supported by Go HTTP frameworks. Or do you ask about modifications done to the global server state?

Global variables in PHP are as global as they are in Go. The difference is that the lifetime of your PHP program is only a single request and started again and again for subsequent requests, that’s why global variables feel as if they were bound to a request only.

Go programs on the other hand are outliving the request and therefore global variables do as well.

I’d pass around a struct containing exactly the values you need. Thinking about every single field you add, since superstructure are as bad as global variables.

1 Like

It’s safe and single user variable until you are declaring and using the variable inside your http.HandleFunc() or other similar functions.

Think your whole php script is just the codeblock inside http.HandleFunc(){/* whole php script */}

So nothing is wrong in golang!

As Norbet and Anik have mentioned, the structure of your program in Go is just different from PHP. In Go you will create the http server in your main func, and your handlers will process code in the area you are used to writing in. The handlers being called with every request and conforming to the nature of stateless HTTP. As Lutz has said if you want data persistence between http requests you will need to implement some form of session state to track your users data.

1 Like

Yeah, connection or session, I’m not trying to be too technical - I’m trying get the idea across which is session level variables. Global variables in PHP don’t really exist like they do in Go AFAICT which if I was to attempt to make a distinction I would call PHP Globals session level variables and Go Globals application level variables.

Since I see below someone else mention the HTTP framework as well that looks like the answer. Thanks.

No, not really. Yes PHP starts and stops per request/session/connection (whatever you want to call it) but the variables are truly isolated. For example, if two people connect simultaneously and I setup a Global var., those are completely separate instances in memory. I don’ think it works the same way in Go. This probably says how it is in PHP clearer than I can:

I know there’s nothing wrong with GoLang, I think it’s a great language and that’s why I’m building an application in it. I have however been PHP centric for a little too long perhaps so I’m taking a while to wrap my mind around a couple of the differences.

So, given this:

http.HandleFunc(){/* whole php script */}

If I was to declare a variable at the top of this, would it exist on a “per request” basis? For example will I have say $User =1 (please ignore the syntax, I’m just talking theoretically) on the first request and $User = 2 on the second without one overwriting the other or am I still missing something in my thinking?

var ProgramBasisGlobal ="this will exist on every request"

http.HandleFunc(w http.ResponseWriter, r *http.Request) {
   ProgramBasisGlobal = "changed!" // this will be changed on every future request on this program!

// below variables will be declared and declined on every request!

    var userBasisVariable = "hello user" //user basis means per request basis
   var  AnotherUserBasisGlobalVariable ="Hello user this is global"

     showMyUsername := func() string{
            username := "Anik" //so called local/private variable
           return username

       //showMyUsername is also a request basis global variable

@AnikHasibul: Outstanding - thank you for the precise clarification. What this tells me is that on a “per user” basis there’s really no way to have such a variable as something that a routine could pick up like a global, it will always have to be specifically a passed variable to the routine that requires it.

This is no problem in my mind, just different to what I am used to so I’ll adjust my thinking accordingly.

Yes, because, both users do get their own instances of the PHP “program”. As I said, PHP globals are as global as they are in Go, its just the lifecycle of the program.

If you were doing CGI with go, you had the same behaviour of globals as in PHP, as there is a separate instance of the go program started for each request.

But you should use correct terms, so we understand each other correctly.

There is so much difference between a connection and a session.

In case of HTTP I do understand a connection as a single request, while a session consists from multiple requests made by the same client.

PHP global variables do not survive a request, so there is no session level global variable. PHP though has a magic variable, which is recreated on each request from the configured session store.

You can use pointer to do that!

Could you elaborate a little? You mean pass a pointer instead of the variable? In this case you are still passing something so there is still a definition on the function for that passed variable so I’m not seeing how this helps remove passing a very common variable around (although pointers have their advantages in their own right.)

That said, the Global command in PHP is essentially setting up a pointer (just not on the function definition) so maybe it’s time I stop pursuing this question.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.