Home >Backend Development >Golang >Why is my Go Conditional Code Not Appending a Library to Apex_Defaults?
Go Conditional Code Not Appending Library to Apex
A developer has encountered an issue where a Go conditional implementation is not appending a library to apex_defaults. Below is the provided code and the proposed solution:
Go Code:
<code class="go">package my_apex import ( "android/soong/android" "android/soong/apex" "fmt" "strings" ) func globalFlags(ctx android.BaseContext) []string { var native_shared_libs []string if strings.Contains(ctx.AConfig().DeviceName(), "my_apex_device") { fmt.Println("Condition executed...") native_shared_libs = append(native_shared_libs, "libabcextractor") } return native_shared_libs } func myApexFlagsDefaults(ctx android.LoadHookContext) { type props struct { Multilib struct { First struct { native_shared_libs []string } } } p := &props{} p.Multilib.First.native_shared_libs = globalFlags(ctx) ctx.AppendProperties(p) } func myApexFlagsDefaultsFactory() android.Module { module := apex.DefaultsFactory() android.AddLoadHook(module, myApexFlagsDefaults) return module } func init() { fmt.Println("Registering module type...") android.RegisterModuleType("my_apex_defaults", myApexFlagsDefaultsFactory) }</code>
Issue:
The conditional code is not appending the library libabcextractor to apex_defaults. The logs indicate that the conditional statement is being executed, but the shared object file is not being generated.
Solution:
The issue lies in the unexported field native_shared_libs in the First struct within the props type used for reflection. In Go, reflection can only access exported struct fields, which are those starting with an uppercase letter.
To resolve this, the developer can change the native_shared_libs field to:
<code class="go">type props struct { Multilib struct { First struct { Native_shared_libs []string } } }</code>
The above is the detailed content of Why is my Go Conditional Code Not Appending a Library to Apex_Defaults?. For more information, please follow other related articles on the PHP Chinese website!