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.
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 }
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
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
There are two problems in your code:
if
/else
clauses) 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 }
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{} }
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{} }
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!