{"action":"created","issue":{"url":"https://api.github.com/repos/golang/go/issues/27075","repository_url":"https://api.github.com/repos/golang/go","labels_url":"https://api.github.com/repos/golang/go/issues/27075/labels{/name}","comments_url":"https://api.github.com/repos/golang/go/issues/27075/comments","events_url":"https://api.github.com/repos/golang/go/issues/27075/events","html_url":"https://github.com/golang/go/issues/27075","id":351850252,"node_id":"MDU6SXNzdWUzNTE4NTAyNTI=","number":27075,"title":"proposal: Go 2: add break and step semantics to the language","user":{"login":"smyrman","id":75205,"node_id":"MDQ6VXNlcjc1MjA1","avatar_url":"https://avatars2.githubusercontent.com/u/75205?v=4","gravatar_id":"","url":"https://api.github.com/users/smyrman","html_url":"https://github.com/smyrman","followers_url":"https://api.github.com/users/smyrman/followers","following_url":"https://api.github.com/users/smyrman/following{/other_user}","gists_url":"https://api.github.com/users/smyrman/gists{/gist_id}","starred_url":"https://api.github.com/users/smyrman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smyrman/subscriptions","organizations_url":"https://api.github.com/users/smyrman/orgs","repos_url":"https://api.github.com/users/smyrman/repos","events_url":"https://api.github.com/users/smyrman/events{/privacy}","received_events_url":"https://api.github.com/users/smyrman/received_events","type":"User","site_admin":false},"labels":[{"id":236419512,"node_id":"MDU6TGFiZWwyMzY0MTk1MTI=","url":"https://api.github.com/repos/golang/go/labels/Proposal","name":"Proposal","color":"ededed","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/golang/go/milestones/30","html_url":"https://github.com/golang/go/milestone/30","labels_url":"https://api.github.com/repos/golang/go/milestones/30/labels","id":1373555,"node_id":"MDk6TWlsZXN0b25lMTM3MzU1NQ==","number":30,"title":"Proposal","description":"Proposals that are pending (not yet accepted/rejected).\r\n","creator":{"login":"rsc","id":104030,"node_id":"MDQ6VXNlcjEwNDAzMA==","avatar_url":"https://avatars1.githubusercontent.com/u/104030?v=4","gravatar_id":"","url":"https://api.github.com/users/rsc","html_url":"https://github.com/rsc","followers_url":"https://api.github.com/users/rsc/followers","following_url":"https://api.github.com/users/rsc/following{/other_user}","gists_url":"https://api.github.com/users/rsc/gists{/gist_id}","starred_url":"https://api.github.com/users/rsc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rsc/subscriptions","organizations_url":"https://api.github.com/users/rsc/orgs","repos_url":"https://api.github.com/users/rsc/repos","events_url":"https://api.github.com/users/rsc/events{/privacy}","received_events_url":"https://api.github.com/users/rsc/received_events","type":"User","site_admin":false},"open_issues":221,"closed_issues":565,"state":"open","created_at":"2015-10-24T01:40:49Z","updated_at":"2018-08-18T20:37:05Z","due_on":null,"closed_at":null},"comments":10,"created_at":"2018-08-18T20:37:03Z","updated_at":"2018-08-18T22:58:55Z","closed_at":null,"author_association":"NONE","body":"This proposal was originally suggested as comments on #25626, but is probably different enough to deserve it's own evaluation. Special thanks to @mccolljr, @smasher164 and @mikeschinkel for the inspiration.\r\n\r\nThere has been many suggestions to help with error handling. This is yet another one... It aims to not sacrificing the current _explicitness_ of error handling in go. It is not however _limited_ to help with error handling as it's a pretty generic syntax extension that may have many use-cases where it could simplify code.\r\n\r\nThe proposal is to add syntax to the Go language to support the cases where you want to _repeat_ the same [Condition](https://golang.org/ref/spec#Condition) check for several _different_ [StatementLists](https://golang.org/ref/spec#StatementList). This is in opposition to the [ForStmt](https://golang.org/ref/spec#ForStmt) where you want to repeat both the [Condition](https://golang.org/ref/spec#Condition) check and the [StatementList](https://golang.org/ref/spec#StatementList). Much like the [SwitchStmt](https://golang.org/ref/spec#SwitchStmt), this new syntax does not necessarily expand what you can express with the language, but it offers a convenient alternative to the [IfStmt](https://golang.org/ref/spec#IfStmt) for a  _specific_ set of use-cases.\r\n\r\nProvitional spec:\r\n\r\n## Break statement\r\n\r\nNew top level definition.\r\n\r\n```\r\n BreakStmt = ExpBreakStmt |  CondBreakStmt .\r\n```\r\n\r\n## Expression breaks\r\n\r\nExisting Break statement from Go 1.\r\n```\r\nExpBreakStmt = \"break\" [ Label ] .\r\n\r\n```\r\n\r\n## Conditional breaks\r\n\r\nThe new syntax and semantics. The idea is that  execution the CondBreakClause will break if Condition is evaluated to false after each CondBreakClause StatementList has finished running. \r\n```\r\nCondBreakStmt = \"break\" Condition  \"{\" { CondBreakClause } \"}\" .\r\nCondBreakClause = \"step\" \":\" StatementList .\r\n```\r\n\r\n# Examples:\r\n\r\nLet's start with a simple example function containing typical Go error handling:\r\n\r\n```go\r\nfunc f() (*Result, error) {\r\n\tr, err := Step1()\r\n\tif err != nil {\r\n\t\treturn nil, fmt.Errorf(\"f failed: %s\", err)\r\n\t}\r\n\r\n\tr, err = Step2(r)\r\n\tif err != nil {\r\n\t\treturn nil, fmt.Errorf(\"f failed: %s\", err)\r\n\t}\r\n\r\n\tr, _ = Step3(r) // ignore errors here...\r\n\tr, err = Step4(r)\r\n\tif err != nil {\r\n\t\treturn nil, fmt.Errorf(\"f failed: %s\", err)\r\n\t}\r\n\r\n        return r, nil \r\n}\r\n```\r\n\r\nThe new syntax allows writing this as:\r\n\r\n```go\r\nfunc f() (*Result, error) {\r\n\tvar err error\r\n\tvar r *Result\r\n\r\n\tbreak err != nil {\r\n\tstep:\r\n\t\tr, err = Step1()\r\n\tstep:\r\n\t\tr, err = Step2(r)\r\n\tstep:\r\n\t\tr, _ = Step3() // ignore errors here\r\n\t\tr, err = Step4(r)\r\n\tstep:\r\n\t\treturn r, nil\r\n\t}\r\n\r\n\treturn nil, fmt.Errorf(\"f failed: %s\", err)\r\n} \r\n```\r\nThe code in the example would execute each step before evaluating the break condition. If the break condition evaluates to `true`.\r\n\r\n\r\nI don't know if it's _right_ to reuse the `break` statement, or if it's better to use another word, e.g. `try` or `guard`; either could work.\r\n\r\nI also don't know if the change offers _significant_ enough benefit to be implemented in Go 2 compared to just using if else, but I suppose the same argument could be held against `switch` / `case`. More examples on where it might be useful might help shed light on that.\r\n\r\nI want to put it on the table people can give feedback on it.\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/golang/go/issues/comments/414091241","html_url":"https://github.com/golang/go/issues/27075#issuecomment-414091241","issue_url":"https://api.github.com/repos/golang/go/issues/27075","id":414091241,"node_id":"MDEyOklzc3VlQ29tbWVudDQxNDA5MTI0MQ==","user":{"login":"jharshman","id":9647217,"node_id":"MDQ6VXNlcjk2NDcyMTc=","avatar_url":"https://avatars0.githubusercontent.com/u/9647217?v=4","gravatar_id":"","url":"https://api.github.com/users/jharshman","html_url":"https://github.com/jharshman","followers_url":"https://api.github.com/users/jharshman/followers","following_url":"https://api.github.com/users/jharshman/following{/other_user}","gists_url":"https://api.github.com/users/jharshman/gists{/gist_id}","starred_url":"https://api.github.com/users/jharshman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jharshman/subscriptions","organizations_url":"https://api.github.com/users/jharshman/orgs","repos_url":"https://api.github.com/users/jharshman/repos","events_url":"https://api.github.com/users/jharshman/events{/privacy}","received_events_url":"https://api.github.com/users/jharshman/received_events","type":"User","site_admin":false},"created_at":"2018-08-18T22:58:55Z","updated_at":"2018-08-18T22:58:55Z","author_association":"NONE","body":"Just my two cents so take it as you would like but I think it's an unneeded feature.\r\nThat being said, I don't think `step` actually adds anything if there was a `break if cond` added to the syntax other than make it kinda hard to read.\r\n\r\nFurthermore, if `step` isn't implemented then why do a `break if cond`?  It's essentially a different method of checking return values after function invocation which to me doesn't flow very well."}}
{"actor":{"display_login":"jharshman"}}