Two more examples to show off some more cool ways match could be useful. Here, I just demonstrate how we can have logic inside matches thanks to anonymous functions:
result = match getDayAndDate() { "tuesday", _: "wow looks like it's tuesday", _, 5: (func() string { if isItWednesday() == true { return "well, at least it's wednesday!" } else { return "not wednesday either!" } })(), } Here's another interesting example which showcases destructuring as well as guards: type date struct { day int month string year int } func getDate() date { /* Use your imagination */ } var today string today = match getDate() { date{day: d, month: m, year: y} if (m == 2) && (d > 29) : fmt.Sprintf("Today is impossible.", m, d, y), date{day: d, month: m, year: y}: fmt.Sprintf("Today is %s %d, %d.", m, d , y), } Nadim Sent from my computer On Wed, Jun 5, 2019 at 6:47 AM Nadim Kobeissi <nadim@nadim.computer> wrote: > Hello everyone, > > This is my first post to this list so please excuse me if I'm not > following all of the proper traditions for this community (posting this in > the wrong place, etc.) > > I like Golang a lot and I think that it's a gift to people who love > computer programming. I have written three softwares in Golang and I'm > really having a great time with this language. > > I was wondering if people would be interested in discussing the inclusion > of pattern matching syntax in Golang. By "pattern matching" I do not mean > regular expressions, but rather a feature that is seen in other languages > such as OCaml > <https://caml.inria.fr/pub/docs/oreilly-book/html/book-ora016.html>. > > Rust also has this feature, which it simply calls "Patterns" > <https://doc.rust-lang.org/1.5.0/book/patterns.html>. In particular > Rust's implementation of this feature seems quite excellent actually. > > I think adding patterns/pattern matching to Golang would allow it to be > used much more seriously in situations like building a parser, transpiler, > compiler etc.. I'm sure there are other uses as well. > > Here is an example of what I have in mind: > > // Just some code to later demonstrate my proposition > func getDayAndDate() (string, int) { > date := getDate() > if todayIsTuesday() == true { > return "tuesday", date > } > return "not tuesday!", date > } > > var result string > > // What I am proposing starts here (the below is currently not valid > Golang code) > result = match getDayAndDate() { > "tuesday", _: "wow looks like it's tuesday", > _, 5: "i don't know if it's tuesday but it's the fifth! nice!", > "not tuesday!", _: "not a tuesday my dude", > } > > Wouldn't that be cool? I don't think that existing Golang syntax allows us > to accomplish stuff like this so elegantly. We'd have to do something > really convoluted. The following doesn't work since switch can't handle > functions with multiple return values: > > switch getDayAndDate() { > case "tuesday", 5: > println("cool!") > } > > switch also can't handle the _ wildcard as a case value. So really there > is no serious alternative to achieve pattern matching in Golang right now, > as far as I can tell. > > I tried searching the Internet, including this mailing list, for previous > discussions of this topic, and the only thing I could find was a post > dating to eight years ago with little follow-up. > > I genuinely believe this is a great feature for Golang to pursue and I > wonder if we can get the developers of this fantastic programming language > and toolkit to take this feature more seriously. I'd be happy to help > implement it myself if I can! > > Thank you, > > Nadim > Sent from my computer > > -- > You received this message because you are subscribed to a topic in the > Google Groups "golang-nuts" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/golang-nuts/JAcxQqVLqUI/unsubscribe. > To unsubscribe from this group and all its topics, 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/2699b3f4-f56d-4e04-bfdb-b00fe10390e0%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/2699b3f4-f56d-4e04-bfdb-b00fe10390e0%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- 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/CAK-38xXYSkE86O7aY%2B4dmdJed4R-yy34mEG9cWms_YOXVum2ow%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.