How can the concept of member variables be the most elegant in non-object-oriented languages?

is actually learning to write a background service in GO recently.

want to rewrite a previous service,

an object is new when a previous request comes in. Then manipulate the object.

within the class, member variables can be defined and shared by all member functions. There are some functions that share these variables.

but when refactoring with GO, because GO has no concept of classes, it feels like something is missing.

define a global variable that will be shared by all requests (not a request to generate a new variable).

do not define global variables, many functions use the same thing, all functions need to constantly return parameters for the next function to use.

it"s always awkward to use structs to simulate classes. How can you modify it to make your code concise and efficient?

I do not know whether the description of the problem is clear or not, and I would like to ask all the bosses for advice.

Nov.19,2021

A

structure is a class. What's wrong with
defining a structure that contains variables that need to be shared, then var a global pointer to the structure, and then new it out when initializing the service?

var srv *Service

type Service struct{
    public map[string]interface{}
}

func InitService(){
    srv = &Service{
        public: make(map[string]interface{}),
    }
}

func main(){
    InitService()
    // TODO
}
Menu