frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

Proposal: Throw as a Special Variable for Auto Error Propagation in Go

2•iondodon•2d ago
Proposal: Implicit Error Propagation via `throw` Identifier in Go

Abstract

This proposal introduces a new syntactic convention to Go: the use of the identifier `throw` in variable declarations or assignments (e.g., `result, throw := errorFunc()`). When detected, the compiler will automatically insert a check for a non-nil error and return zero values for all non-error return values along with the error. This mechanism streamlines error handling without compromising Go's hallmark of explicit, readable code.

Motivation

Go encourages explicit error handling, which often results in repetitive boilerplate code. For example:

result, err := errorFunc() if err != nil { return zeroValue, err }

This pattern, while clear, adds verbosity that can hinder readability, especially in functions with multiple error-prone calls. By introducing a syntactic shorthand that preserves clarity, we can reduce boilerplate and improve developer ergonomics.

Proposal

When a variable named `throw` is assigned the result of a function returning an `error`, and the enclosing function returns an `error`, the compiler will implicitly insert:

if throw != nil { return zeroValues..., throw }

Applicable Scenarios

Short declarations:

  x, throw := doSomething()
Standard assignments:

  x, throw = doSomething()
Variable declarations with assignment:

  var x T; var throw error; x, throw = doSomething()


* `throw` must be a variable of type `error`

* The surrounding function must return an `error`

* The rule only applies when the variable is explicitly named `throw`

Example

Traditional Error Handling

func getUserData(id int) (data Data, err error) { data, err := fetch(id) if err != nil { return Data{}, err } return data, nil }

With `throw`

func getUserData(id int) (Data, error) {

    data, throw := fetch(id)
    // Automatically expands to: if throw != nil { return Data{}, throw }

    moreData, throw := fetchMore(id)
    // Automatically expands to: if throw != nil { return Data{}, throw }

    return data, nil
}

Comments

NoahZuniga•1d ago
While I have no experience with go, this seems like an interesting idea. Hackernews however isn't a great place to post this proposal if you want it to be implemented.

Maybe take a look at https://github.com/golang/proposal and if you want to share your proposal with hn, submit a link to the proposal here.

Ask HN: Has anybody built search on top of Anna's Archive?

26•neonate•2h ago•4 comments

Ask HN: Options for One-Handed Typing

79•Townley•9h ago•80 comments

Ask HN: What's with the repeated job posts on "Who's hiring"?

67•rafavento•11h ago•32 comments

Ask HN: Who is hiring? (June 2025)

363•whoishiring•1d ago•403 comments

Ask HN: How do I learn robotics in 2025?

376•srijansriv•1d ago•95 comments

Ask HN: How do I learn practical electronic repair?

159•juanse•3d ago•100 comments

Ask HN: Who wants to be hired? (June 2025)

122•whoishiring•1d ago•343 comments

Reaching my first 100 users without money or audience (at 10K users now)

23•felixheikka•14h ago•6 comments

Ask HN: Anyone making a living from a paid API?

240•meander_water•3d ago•169 comments

Ask HN: Resources for building AI agents for software development?

4•nadis•9h ago•3 comments

Ask HN: Most (Writing) Tools Are AI-Enabled, Not AI-First. What's Still Missing?

3•Danao•9h ago•2 comments

Ask HN: How do you struggle with/optimize for deep,sustained focus?

5•jenever•10h ago•3 comments

Ask HN: How are parents who program teaching their kids today?

97•laze00•2d ago•91 comments

Ask HN: What is the best LLM for consumer grade hardware?

237•VladVladikoff•4d ago•180 comments

Opening up my SaaS's API turned out to be one of the smartest growth decisions

5•usamaejaz•15h ago•0 comments

Ask HN: Those making $500/month on side projects in 2025 – Show and tell

17•fazkan•9h ago•4 comments

Google DMARC Policy Changes?

32•flajr•1d ago•9 comments

Ask HN: Cursor is productive for an hour, then burns my application down

7•campervans•1d ago•5 comments

Ask HN: Is offering a one-time payment stupid?

10•bigscrankus•1d ago•35 comments

Built an AI tool to visualize large codebases - would love feedback

13•ilia_khatko•1d ago•9 comments

Understanding physical attacks on Android phones

5•alokq•1d ago•2 comments

Ask HN: What is your current LLM-assisted coding tool?

7•HiPHInch•1d ago•4 comments

Ask HN: How do startups create fancy websites?

5•ciwolex•1d ago•5 comments

Ask HN: Should science ensure NDEs are pleasant without damaging the brain?

2•amichail•1d ago•3 comments

My "tiny" Product Hunt alternative made $5.6k in revenue last month

5•jaisalrathee•1d ago•2 comments

Ask HN: Local AI for Handwriting Transcription?

4•i4i•1d ago•1 comments

Ask HN: Who is using C?

8•00taffe•12h ago•8 comments

Ask HN: What should you ask to understand a new team and company?

8•laura2013•2d ago•6 comments

Ask HN: Why reinvent front-end frameworks and static site builders?

7•keepamovin•3d ago•13 comments

CQRS and Event Sourcing for the Rest of Us

7•odinellefsen•2d ago•1 comments