How to define own struct on substrate ink - substrate

I want to know how to define own struct.
I referred Substrate ink! Sample,but I could not find it.

I write sample code as following.
https://realtakahashi-work.medium.com/substrate-ink-how-to-define-own-struct-%E7%8B%AC%E8%87%AA%E6%A7%8B%E9%80%A0%E4%BD%93%E3%81%AE%E5%AE%A3%E8%A8%80%E6%96%B9%E6%B3%95-8f4893089ba4

Related

Why are unexported struct fields purposely not marshalled in the json package

In the json package if you want to utilize tags to marshall and unmarshall structs the field must be exported. I always thought this was a limitation on the reflect package but it seems to be explicitly rejected here.
I would like to know what I am missing or the design choices behind this as it creates one of the following issues normally.
Exporting more fields than I want allowing users to change values without using potential getters and setters.
A lot of extra code if you chose to solve the above problem by making a second identical struct just for tag usage and make it unexported.
Here is a playground of code being able to read unexported tags.
Edit: Better playground thanks to #mkopriva

Can we dynamically create a function in go?

I'm trying to create a service where a user needs to operate over a data and can manipulate it in numerous ways, so I'm not aware of the manipulations at the time of compiling of my program. One way to achieve this is to give the user a function with data as param. Which landed me in the following direction.
Dynamically create a function
Dynamically linking a function after compiling it separately.
I'm open to suggestions. If you have other ways to achieve the end goal.
If you don't like this as an answer I can move to the comment section, but it's rather long that's why I put here in the answer section.
Dynamically Dispatched Method: The only way to have dynamically dispatched methods is through an interface. Methods on a struct or any other concrete type are always resolved statically.
Closure: Go supports anonymous functions, which can form closures. Anonymous functions are useful when you want to define a function inline without having to name it.
Dyncamically call method on Interface:
Please let me know if that helps you to understand the concept in golang.

Reflect struct with struct methods

I'm playing around with reflect and I'm trying to reflect a struct, create a new one and try to call it.
The variables are working fine but when I reflect a new struct, the struct methods are not copied?
I created a simple example on the playground. On line 34 I receive that 0 Methods exist, but there should be 1 (SetName). Am I doing something wrong? already google for it since hours but do not get any solutions.
https://play.golang.org/p/yArjVLtWEaG
Thanks in advance
cheers pat
SetName is not a method of type company but of type *company. Sou you must create a pointer to company.

Get names of structs that implement an interface or inherit a struct

Is it possible to get a slice of strings that represent the names of all types that implement an interface or inherit from a specific struct in a specific package using reflection?
After some research on the reflect package's doc, I don't think it's possible. That's not the way reflection work in go: the interfaces mechanism not beeing declarative (but duck-typed instead), there is no such list of types.
That said, you may have more luck using the ast package to parse your project, get the list of types, and check wheter or not they implement an interface, then write some code to give you the said slice. That would add a step to compilation, but could work like a charm.
AFAIK, you can't do this with reflect, since packages are kinda out of reflect's scope.
You can do this the same way godoc's static analysis works. That is, using code.google.com/p/go.tools/go/types to parse the package's source code and get the type info.
The go oracle can do this. https://godoc.org/code.google.com/p/go.tools/oracle
Here is the relevant section of the user manual.

Manipulate struct directly or via return

I noticed that I am not consistent when writing golang regarding the following question: Is it better to manipulate a struct in its methods via the struct itself directly or by using return? Since this question sounds stupid, here's an example:
Manipulate a struct directly
http://play.golang.org/p/7G5D8Pm5wv
Via return
http://play.golang.org/p/L9Z_t7pA8b
Please explain why one attempt is better than the other. An if both somehow wrong, please give an example that would be ok.
Thanks in advance!
I think it depends on how you use your struct. If you use it as an object i.e. there function with receivers of that type I think you should manipulate the state (the fields of the struct) with functions too. If you use the struct purely as a data store, then manipulating the fields directly seems more OK.
Read
Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin, chapter 6 on Objects & Data structures
Google for that book and you will find online versions.
and
http://en.wikipedia.org/wiki/Law_of_Demeter

Resources