Create a GO structure only if certain conditions are met

WBOY
Release: 2024-02-08 21:05:49
forward
1005 people have browsed it

仅当满足特定条件时才创建 GO 结构

php editor Zimo will introduce you to an important feature, which is the creation of GO structures under specific conditions. This feature allows us to determine whether to create a GO structure as needed, thereby improving the efficiency and performance of the program. By using this feature rationally, we can avoid unnecessary creation of GO structures, reduce memory usage and garbage collection pressure, and improve the running efficiency of the program. In this article, we will detail how to use this feature and give some examples of practical applications.

Question content

My go code snippet is as follows:

type mystruct struct {
   a int
}

if a == nil {
        cond = 0
        var a_mystruct_obj mystruct  // if this condition is satified then only create this a_mystruct_obj obj
    } else if b == nil {
        cond = 1
        var b_mystruct_obj mystruct  // if this condition is satified then only create this b_mystruct_obj obj

    } else {
        cond = 2 // // if this condition is satified then create both the above structure objects  a_mystruct_obj & b_mystruct_obj.
         // is having the below declaration again in else a valid go code ?
        var a_mystruct_obj mystruct
        var b_mystruct_obj mystruct
    }
Copy after login

I have c background. This would be simple in c. Is there dynamic memory allocation in go? How can I achieve this in go?

Is it valid go code to declare 2 again in else?

var A_mystruct_obj MyStruct
    var B_mystruct_obj MyStruct
Copy after login

Or do I need to have some kind of runtime polymorphism here.

renew:

I tried doing this but it didn't even compile as expected.

https://go.dev/play/p/ioq81aexgjn

Update again

I tried this and it seems to work. Is this okay?

https://go.dev/play/p/r_ywzmkgrps


Correct answer


There are two problems in your code:

  1. Your variables are declared in an enclosing scope, so they are not accessible from outside that scope (if/else clauses)
  2. Your variable is declared as a value

When you solve the first problem, that is by moving the variable declarations outside the scope of the if clause so that they can be accessed by the code following the if statement:

    var a_mystruct_obj mystruct
    var b_mystruct_obj mystruct
    if a == nil {
        cond = 0
        // initialise a_mystruct
    } else if b == nil {
        cond = 1
        // initialise b_mystruct
    } else {
        cond = 2
        // initialise a_mystruct and b_mystruct
    }
Copy after login

Now both variables are declared, and both variables are initialized with the newly allocated mystruct regardless of which clause in the if statement is reached.

To solve this problem, change the variable to a pointer and assign the desired value in the corresponding branch of the if statement:

    var a_mystruct_obj *mystruct
    var b_mystruct_obj *mystruct
    if a == nil {
        cond = 0
        a_mystruct_obj = &mystruct{}
    } else if b == nil {
        cond = 1
        b_mystruct_obj = &mystruct{}
    } else {
        cond = 2
        a_mystruct_obj = &mystruct{}
        b_mystruct_obj = &mystruct{}
    }
Copy after login

Bonus Tip: Future you will thank you Refactor this into a pair of statements, determine if you need a or b or Both, then simplify your compound if statement as two separate statements, initializing a or b respectively:

    var A_mystruct_obj *MyStruct
    var B_mystruct_obj *MyStruct

    areq := a == nil || (..condition 2..) 
    breq := b == nil || (..condition 2..) 
    if areq {
        A_mystruct_obj = &MyStruct{}
    }
    if breq {
        B_mystruct_obj = &MyStruct{}
    }
Copy after login

The purpose is to avoid logical duplication (dry principle: Don’t repeat yourself) and separation of concerns. That is, separate the question of whether a and/or b is needed from the question of when initialization of a and b is needed. em> Required.

Note: It's not possible to be more specific about how to express condition 2, since it's not clear from the code you posted what the condition is.

The above is the detailed content of Create a GO structure only if certain conditions are met. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!