following simulates some functionality package main
import ( "fmt" "io" "log" "os" ) func GOTO(f *os.File, e error) (*os.File, error) { return f, e } func COPYGOTO(i int64, e error) (int64, error) { return i, e } var err error var r, w *os.File func main() { log.Println(Experimental("test.txt", "text.txt")) } func Experimental(src, dst string) error { //goto jumps to "label" if present; selects values right to left; is ignored with value´s zero value r, err = GOTO(os.Open(src)) if err != nil { goto err } defer r.Close() w, err = GOTO(os.Create(dst)) if err != nil { goto err } defer w.Close() _, err = COPYGOTO(io.Copy(w, r)) if err != nil { goto err } err = w.Close() if err != nil { goto err } return nil err: defer os.Remove(dst) return fmt.Errorf("copy %s %s: %v", src, dst, err) } Matthias Mädel schrieb am Donnerstag, 14. April 2022 um 11:05:15 UTC+2: > some refining... > > func Experimental(src, dst string) error { > > //goto jumps to "label" if present; selects values right to left; is > ignored with value´s zero value > goto r, err := os.Open(src) > defer r.Close() > goto w, err := os.Create(dst) > defer w.Close() > goto v, err := io.Copy(w, r) > goto err := w.Close() > > //new: a "label" is a block also holding pointer to the actual value > v: { > //do something with value here... > //return error > } > > err:{ > defer os.Remove(dst) > return fmt.Errorf("copy %s %s: %v", src, dst, err) > } > > return nil > > } > > Ian Lance Taylor schrieb am Donnerstag, 14. April 2022 um 00:04:22 UTC+2: > >> On Wed, Apr 13, 2022 at 2:44 PM Matthias Mädel >> <medienwer...@gmail.com> wrote: >> > >> > Please some advice if something like the following could be helpful and >> if it is possible to implement with the compiler. >> > >> > Thank You >> > >> > func Experimental(src, dst string) error { >> > >> > //goto is blocked with err == nil >> > goto r, err := os.Open(src) >> > defer r.Close() >> > goto w, err := os.Create(dst) >> > goto v, err := io.Copy(w, r) >> > goto err := w.Close() >> > return nil >> > >> > //alternative >> > goto err { >> > r, err := os.Open(src) >> > defer r.Close() >> > w, err := os.Create(dst) >> > v, err := io.Copy(w, r) >> > err := w.Close() >> > return nil >> > } >> > >> > return err: >> > w.Close() >> > os.Remove(dst) >> > fmt.Errorf("copy %s %s: %v", src, dst, err) >> >> Has some similarities to >> >> https://go.dev/issue/32611 >> https://go.dev/issue/34140 >> https://go.dev/issue/37035 >> >> Ian >> > -- 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/ca7afb02-738a-402e-a2ac-591ad68d8c95n%40googlegroups.com.