Re: [go-nuts] reflect Call method on nested struct
Hi Max, this makes the config even simpler since i do not need to be sure that the field is nil. I will try this out for sure. On Wednesday, May 29, 2019 at 1:56:14 PM UTC+3, Max wrote: > > There is another improvement you can do: you are currently using > > cfg := Config{Name: } > r := reflect.ValueOf(cfg) > > which means that 'r' will contain a copy of 'cfg' and will not be settable. > If instead you use: > > r := reflect.ValueOf().Elem() > > then r will contain a *pointer* to the original 'cfg' and can modify it > (the reflect.Value 'r' is settable). > In turn, this also allows to declare 'Config' as follows: > ``` > type Config struct { > Name String > } > ``` > i.e. removes the need to use a *pointer* to String. > You also need some minor adjustments to the rest of the code... > > A complete example is: > > https://play.golang.org/p/AxPE0K_ivxP > > On Wednesday, May 29, 2019 at 11:56:42 AM UTC+2, Sotirios Mantziaris wrote: >> >> Ok, found it. I should have used the reflect.ValueOf... >> >> On Wednesday, May 29, 2019 at 12:53:13 PM UTC+3, Sotirios Mantziaris >> wrote: >>> >>> I did found out that the setter method has to accept a pointer to string. >>> I have a complete example now which unfortunately don't work correclty. >>> The value should change but it is emptied out. >>> >>> https://play.golang.org/p/OPZKltApEhF >>> >>> What am i missing? >>> >>> On Wednesday, May 29, 2019 at 10:46:32 AM UTC+3, Jan Mercl wrote: On Wed, May 29, 2019 at 9:42 AM Sotirios Mantziaris wrote: > I am getting the nested "Name" struct but there are no methods to call. > What am i doing wrong (besides using reflection :))? The method has a pointer receiver: https://play.golang.org/p/qjhqSvhE9PL >>> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f121de4b-95a3-4a9b-9084-7672a8e20d34%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] reflect Call method on nested struct
There is another improvement you can do: you are currently using cfg := Config{Name: } r := reflect.ValueOf(cfg) which means that 'r' will contain a copy of 'cfg' and will not be settable. If instead you use: r := reflect.ValueOf().Elem() then r will contain a *pointer* to the original 'cfg' and can modify it (the reflect.Value 'r' is settable). In turn, this also allows to declare 'Config' as follows: ``` type Config struct { Name String } ``` i.e. removes the need to use a *pointer* to String. You also need some minor adjustments to the rest of the code... A complete example is: https://play.golang.org/p/AxPE0K_ivxP On Wednesday, May 29, 2019 at 11:56:42 AM UTC+2, Sotirios Mantziaris wrote: > > Ok, found it. I should have used the reflect.ValueOf... > > On Wednesday, May 29, 2019 at 12:53:13 PM UTC+3, Sotirios Mantziaris wrote: >> >> I did found out that the setter method has to accept a pointer to string. >> I have a complete example now which unfortunately don't work correclty. >> The value should change but it is emptied out. >> >> https://play.golang.org/p/OPZKltApEhF >> >> What am i missing? >> >> On Wednesday, May 29, 2019 at 10:46:32 AM UTC+3, Jan Mercl wrote: >>> >>> On Wed, May 29, 2019 at 9:42 AM Sotirios Mantziaris >>> wrote: >>> >>> > I am getting the nested "Name" struct but there are no methods to >>> call. >>> > What am i doing wrong (besides using reflection :))? >>> >>> The method has a pointer receiver: https://play.golang.org/p/qjhqSvhE9PL >>> >> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/b7ac54f5-515c-448d-84db-f58ee7e80697%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] reflect Call method on nested struct
Ok, found it. I should have used the reflect.ValueOf... On Wednesday, May 29, 2019 at 12:53:13 PM UTC+3, Sotirios Mantziaris wrote: > > I did found out that the setter method has to accept a pointer to string. > I have a complete example now which unfortunately don't work correclty. > The value should change but it is emptied out. > > https://play.golang.org/p/OPZKltApEhF > > What am i missing? > > On Wednesday, May 29, 2019 at 10:46:32 AM UTC+3, Jan Mercl wrote: >> >> On Wed, May 29, 2019 at 9:42 AM Sotirios Mantziaris >> wrote: >> >> > I am getting the nested "Name" struct but there are no methods to call. >> > What am i doing wrong (besides using reflection :))? >> >> The method has a pointer receiver: https://play.golang.org/p/qjhqSvhE9PL >> > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/e2eb5584-242c-4144-ad32-a42583ee8cfe%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] reflect Call method on nested struct
I did found out that the setter method has to accept a pointer to string. I have a complete example now which unfortunately don't work correclty. The value should change but it is emptied out. https://play.golang.org/p/OPZKltApEhF What am i missing? On Wednesday, May 29, 2019 at 10:46:32 AM UTC+3, Jan Mercl wrote: > > On Wed, May 29, 2019 at 9:42 AM Sotirios Mantziaris > > wrote: > > > I am getting the nested "Name" struct but there are no methods to call. > > What am i doing wrong (besides using reflection :))? > > The method has a pointer receiver: https://play.golang.org/p/qjhqSvhE9PL > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/001a34f6-163e-45fa-84ce-b10214b4%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] reflect Call method on nested struct
you are a live saver. thanks very much. reflection is of of these things in any language i suppose. On Wed, 29 May 2019 at 10:46, Jan Mercl <0xj...@gmail.com> wrote: > On Wed, May 29, 2019 at 9:42 AM Sotirios Mantziaris > wrote: > > > I am getting the nested "Name" struct but there are no methods to call. > > What am i doing wrong (besides using reflection :))? > > The method has a pointer receiver: https://play.golang.org/p/qjhqSvhE9PL > -- Regards, S. Mantziaris -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEGsCe2AfWreDBbs4OJy8xSxi0Xs-ibxw%3DB4X%2BRwSJpwx60TNg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] reflect Call method on nested struct
On Wed, May 29, 2019 at 9:42 AM Sotirios Mantziaris wrote: > I am getting the nested "Name" struct but there are no methods to call. > What am i doing wrong (besides using reflection :))? The method has a pointer receiver: https://play.golang.org/p/qjhqSvhE9PL -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAA40n-XhXgHagW5wDg%3DuFi2mc6Q%3DoKUrCX01wOFx6aEo2e0O7Q%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] reflect Call method on nested struct
Hi, i have a nested struct and i want to call a method on a field. type String struct { value string } func (s *String) Set(value string) { s.value = value } type Config struct { Name String } I am getting the nested "Name" struct but there are no methods to call. What am i doing wrong (besides using reflection :))? Check out the playground link? https://play.golang.org/p/XSRG5rFWdhq -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/83c973f9-429d-4b8b-a7a6-31fd048b9c7e%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.