Go Details & Tips 101 (20220829) by Tapir Liu
Author:Tapir Liu
Language: eng
Format: epub
The famous := trap
Let's view a simple program.
package main import "fmt" import "strconv" func parseInt(s string) (int, error) { n, err := strconv.Atoi(s) if err != nil { b, err := strconv.ParseBool(s) if err != nil { return 0, err } if b { n = 1 } } return n, err } func main() { fmt.Println(parseInt("true")) }
We know that the call strconv.Atoi(s) will return a non-nil error, but the call strconv.ParseBool(s) will return a nil error. Then, will the call parseInt("true") return a nil error, too? The answer is it will return a non-nil error.
Wait, isn't the err variable is re-declared in the inner code block and its value has been modified to nil before the parseInt("true") returns? This is a confusion many new Go programmers, including me, ever encountered when they just started using Go.
The reason why the call parseInt("true") returns a non-nil error is a variable declared in an inner code block is never a re-declaration of a variable declared in an outer code block. Here, the inner declared err variable is set (initialized) as nil. It is not a re-declaration (a.k.a. modification) of the outer declared err variable. The outer one is set (initialized) as a non-nil value, then it is never changed later.
There is the voice to remove the ... := ... re-declaration syntax form from Go. But it looks this is a too big change for Go. Personally, I think explicitly marking the re-declared variables out is a more feasible solution.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Navigation and Map Reading by K Andrew(4891)
Spare by Prince Harry The Duke of Sussex(4791)
Tuesdays with Morrie by Mitch Albom(4407)
Cracking the GRE Premium Edition with 6 Practice Tests, 2015 (Graduate School Test Preparation) by Princeton Review(4049)
Machine Learning at Scale with H2O by Gregory Keys | David Whiting(3640)
What It Really Takes to Get Into Ivy League and Other Highly Selective Colleges by Hughes Chuck(3554)
Never by Ken Follett(3532)
Goodbye Paradise(3452)
Harry Potter and the Prisoner of Azkaban (Book 3) by J. K. Rowling(3113)
Pledged by Alexandra Robbins(3049)
Kick Ass in College: Highest Rated "How to Study in College" Book | 77 Ninja Study Skills Tips and Career Strategies | Motivational for College Students: A Guerrilla Guide to College Success by Fox Gunnar(2998)
Fairy Tale by Stephen King(2952)
A Dictionary of Sociology by Unknown(2858)
Sapiens and Homo Deus by Yuval Noah Harari(2845)
Reminders of Him: A Novel by Colleen Hoover(2769)
The Social Psychology of Inequality by Unknown(2768)
Graduate Admissions Essays, Fourth Edition: Write Your Way into the Graduate School of Your Choice (Graduate Admissions Essays: Write Your Way Into the) by Asher Donald(2736)
Get into Any College by Tanabe Gen Tanabe Kelly(2632)
Zero to Make by David Lang(2629)
