Hi,
I want to create a variable and a simple function programmatically.
How can I achieve it.
Hi,
I want to create a variable and a simple function programmatically.
How can I achieve it.
Why do you want to do that? And what do you mean by programmatically, exactly?
I want to create a framework in golang, using which I want to create functions and variables dynamically and store it in memory, to make a product which is more generic.
Again, the question is what you want to accomplish. You cannot generate new code during runtime if this is what you mean by creating functions programmatically. However there are likely other ways to solve your actual problem, which we could guide you towards if you explained what it is.
Ok, I will explain clearly. kindly guide me. or tell me if I am wrong.
I want to create a frame work for satisfying followingâŚ
I want to create a core - If a customer comes to me with a requirement, I donât want to create my service or logic from the scratch. along with the core I will be having a UI with drag and drop functionality, using which entire code and logic for the requirement should be created.
this is he basic idea of what I am thinking.
what many suggested me is, using file writing create .go files with all necessary structs, functions, variables, etcâŚ
but what I am looking is instead of creating it programmatically and keeping it in memory .and using it as necessarily.
something like this.
NewFunc(funcName string, signature []string, return []string)
eg. Newfunc(âaddâ,[âintâ ,âintâ],[âintâ]) -> creates a function with name- add, with 2 arguments as int and with a return type int.
NewVariable(variableName String, Type String)
eg. NewVariable(âTempâ,âStringâ) -> creates a variable with name Temp and type String
Kindly suggest me an idea
What you want to do isnât possible. You can indeed do code generation where you generate the Go code, compile it, and maybe load it as a plugin.
code generation means creating go files⌠programmatically. is itâŚ
can I do anything with use of go/types package?
I guess your approach is totally wrong. You seem to want implenting business logic as code. I donât see the point why it should not be easier to have your âframeworkâ and extend it programatically. Or use it as a dependency.
You cannot do like this because your code need to be compiled.
Maybe you can achieve your goal with some dynamic languages such as Python.
You can probably use the âreflectâ package to do most of what you want to do:
But, as the other posters implied, your package wouldnât look like idiomatic Go code.
Reflection is slow.
Reflection makes for very dynamic code which may end up being very hard to reason about and debug.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.