Re: [go-nuts] Is there a way to cast interface to embedded type?
Thanks to both of you. I'll try out your suggestions. On Friday, February 9, 2024 at 4:35:31 PM UTC-5 Mike Schinkel wrote: > On Feb 9, 2024, at 3:37 PM, Christopher C wrote: > > I have a base struct that implements an interface. There are multiple > other structs that embed this base struct. I would like to pass the an > interface into a function that can cast it as the base struct and call some > functions tied to the base struct. > > Something like this... > https://go.dev/play/p/DUzXr31s8Pn > > > You can't cast like in your example, but you can create an interface — > call is `Baser` using idiomatic interface naming to identify the `Base` > type which will have an empty `Base()` method — and then type assert to it > after which, if it succeeds you can type assert to `*Base`, like so: > > https://go.dev/play/p/-gcKGf4_AFg > > Hope this helps. > > -Mike > > -- 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/5c28ebf6-e998-45ae-b87d-eae2dbb83833n%40googlegroups.com.
[go-nuts] Is there a way to cast interface to embedded type?
I have a base struct that implements an interface. There are multiple other structs that embed this base struct. I would like to pass the an interface into a function that can cast it as the base struct and call some functions tied to the base struct. Something like this... https://go.dev/play/p/DUzXr31s8Pn -- 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/1ded992c-3f90-4c30-99a5-532e573cf16fn%40googlegroups.com.
[go-nuts] Re: Handling EOF when using json.NewDecoder() from named pipe
I was thinking partial reads could be an issue and the Decoder seemed to do the initial checking for me. Would the ReadAll() be able to recover from EOF state? On Tuesday, October 17, 2023 at 4:37:58 AM UTC-4 Volker Dobler wrote: > Why do you use a json.Decoder? It seems as reading > everything (io.ReadAll) until EOF and json.Unmarshal'ling > would be a cleaner/simpler solution? > > V. > > On Tuesday, 17 October 2023 at 09:10:09 UTC+2 Christopher C wrote: > >> Hello all! >> I'm trying to read json objects from a named pipe. The pipe will be >> filled intermittently by bash scripts. After the Decode() of the first >> object, any more calls to Decode() will return EOF. This seems proper >> since the script has completed, but once it errors with EOF, there doesn't >> seem to be a way to read any more. >> >> Is there a way to 'reset' the decoder so when another script writes to >> the pipe it can process the next object, or should I be doing some pipe >> length validation before trying to decode? >> >> Current read code snippet is... >> >> decoder := json.NewDecoder(fpipe) >> for { >> err := decoder.Decode(&msg) >> if err != nil { >> if err == io.EOF { >> // how to reset this? >> } else { >> logger.Fatal(err) >> } >> } else { >> // send out the msg >> >> -- 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/8446c446-2113-4c65-bf3c-5a7f84c601dcn%40googlegroups.com.
[go-nuts] Handling EOF when using json.NewDecoder() from named pipe
Hello all! I'm trying to read json objects from a named pipe. The pipe will be filled intermittently by bash scripts. After the Decode() of the first object, any more calls to Decode() will return EOF. This seems proper since the script has completed, but once it errors with EOF, there doesn't seem to be a way to read any more. Is there a way to 'reset' the decoder so when another script writes to the pipe it can process the next object, or should I be doing some pipe length validation before trying to decode? Current read code snippet is... decoder := json.NewDecoder(fpipe) for { err := decoder.Decode(&msg) if err != nil { if err == io.EOF { // how to reset this? } else { logger.Fatal(err) } } else { // send out the msg -- 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/0846d26b-8cd3-4d1f-bebb-9972ea248c30n%40googlegroups.com.