how return is working in this tour of go example?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
this is my first week learning Go. I am checking Tour of Go and in the code in https://tour.golang.org/flowcontrol/5 I don't understand how return is working inside sqrt() function.
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
}
return fmt.Sprint(math.Sqrt(x))
}
I understand the code with a else clause like this
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
}else{
return fmt.Sprint(math.Sqrt(x))}
}
this code is executed without problems, but the golint in VsCode suggests me to drop the else clause.
Does return sqrt(-x) + "i" inside the if ends executing the function or how is it exactly working? Thanks in advance.
go
add a comment |
this is my first week learning Go. I am checking Tour of Go and in the code in https://tour.golang.org/flowcontrol/5 I don't understand how return is working inside sqrt() function.
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
}
return fmt.Sprint(math.Sqrt(x))
}
I understand the code with a else clause like this
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
}else{
return fmt.Sprint(math.Sqrt(x))}
}
this code is executed without problems, but the golint in VsCode suggests me to drop the else clause.
Does return sqrt(-x) + "i" inside the if ends executing the function or how is it exactly working? Thanks in advance.
go
add a comment |
this is my first week learning Go. I am checking Tour of Go and in the code in https://tour.golang.org/flowcontrol/5 I don't understand how return is working inside sqrt() function.
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
}
return fmt.Sprint(math.Sqrt(x))
}
I understand the code with a else clause like this
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
}else{
return fmt.Sprint(math.Sqrt(x))}
}
this code is executed without problems, but the golint in VsCode suggests me to drop the else clause.
Does return sqrt(-x) + "i" inside the if ends executing the function or how is it exactly working? Thanks in advance.
go
this is my first week learning Go. I am checking Tour of Go and in the code in https://tour.golang.org/flowcontrol/5 I don't understand how return is working inside sqrt() function.
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
}
return fmt.Sprint(math.Sqrt(x))
}
I understand the code with a else clause like this
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
}else{
return fmt.Sprint(math.Sqrt(x))}
}
this code is executed without problems, but the golint in VsCode suggests me to drop the else clause.
Does return sqrt(-x) + "i" inside the if ends executing the function or how is it exactly working? Thanks in advance.
go
go
asked Nov 29 '18 at 1:25
Leonardo Medina CorillocllaLeonardo Medina Corilloclla
6
6
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
As with most (if not all?) the first return statement the compiler hits will exit the function and not continue.
add a comment |
It's warning reported by go linter. Code underneath is valid.
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
}
return fmt.Sprint(math.Sqrt(x))
}
This one below is also valid, but will generate some warning.
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
} else {
return fmt.Sprint(math.Sqrt(x))
}
}
Basically if there is if statement and the body contains return statement, better not to use else block.
add a comment |
The Go Programming Language Specification
Return statements
A "return" statement in a function F terminates the execution of F,
and optionally provides one or more result values. Any functions
deferred by F are executed before F returns to its caller.
The specification defines the language. return terminates the function.
Go Code Review Comments
Indent Error Flow
Try to keep the normal code path at a minimal indentation, and indent
the error handling, dealing with it first. This improves the
readability of the code by permitting visually scanning the normal
path quickly. For instance, don't write:
if err != nil {
// error handling
} else {
// normal code
}
Instead, write:
if err != nil {
// error handling
return // or continue, etc.
}
// normal code
While either will work, as a matter of style, remove the unnecessary else and indentation. It's a similar to error flow indentation.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53530536%2fhow-return-is-working-in-this-tour-of-go-example%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
As with most (if not all?) the first return statement the compiler hits will exit the function and not continue.
add a comment |
As with most (if not all?) the first return statement the compiler hits will exit the function and not continue.
add a comment |
As with most (if not all?) the first return statement the compiler hits will exit the function and not continue.
As with most (if not all?) the first return statement the compiler hits will exit the function and not continue.
answered Nov 29 '18 at 1:29
TheValyreanGroupTheValyreanGroup
2,8872726
2,8872726
add a comment |
add a comment |
It's warning reported by go linter. Code underneath is valid.
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
}
return fmt.Sprint(math.Sqrt(x))
}
This one below is also valid, but will generate some warning.
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
} else {
return fmt.Sprint(math.Sqrt(x))
}
}
Basically if there is if statement and the body contains return statement, better not to use else block.
add a comment |
It's warning reported by go linter. Code underneath is valid.
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
}
return fmt.Sprint(math.Sqrt(x))
}
This one below is also valid, but will generate some warning.
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
} else {
return fmt.Sprint(math.Sqrt(x))
}
}
Basically if there is if statement and the body contains return statement, better not to use else block.
add a comment |
It's warning reported by go linter. Code underneath is valid.
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
}
return fmt.Sprint(math.Sqrt(x))
}
This one below is also valid, but will generate some warning.
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
} else {
return fmt.Sprint(math.Sqrt(x))
}
}
Basically if there is if statement and the body contains return statement, better not to use else block.
It's warning reported by go linter. Code underneath is valid.
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
}
return fmt.Sprint(math.Sqrt(x))
}
This one below is also valid, but will generate some warning.
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
} else {
return fmt.Sprint(math.Sqrt(x))
}
}
Basically if there is if statement and the body contains return statement, better not to use else block.
answered Nov 29 '18 at 1:32
xparexpare
4,7542252
4,7542252
add a comment |
add a comment |
The Go Programming Language Specification
Return statements
A "return" statement in a function F terminates the execution of F,
and optionally provides one or more result values. Any functions
deferred by F are executed before F returns to its caller.
The specification defines the language. return terminates the function.
Go Code Review Comments
Indent Error Flow
Try to keep the normal code path at a minimal indentation, and indent
the error handling, dealing with it first. This improves the
readability of the code by permitting visually scanning the normal
path quickly. For instance, don't write:
if err != nil {
// error handling
} else {
// normal code
}
Instead, write:
if err != nil {
// error handling
return // or continue, etc.
}
// normal code
While either will work, as a matter of style, remove the unnecessary else and indentation. It's a similar to error flow indentation.
add a comment |
The Go Programming Language Specification
Return statements
A "return" statement in a function F terminates the execution of F,
and optionally provides one or more result values. Any functions
deferred by F are executed before F returns to its caller.
The specification defines the language. return terminates the function.
Go Code Review Comments
Indent Error Flow
Try to keep the normal code path at a minimal indentation, and indent
the error handling, dealing with it first. This improves the
readability of the code by permitting visually scanning the normal
path quickly. For instance, don't write:
if err != nil {
// error handling
} else {
// normal code
}
Instead, write:
if err != nil {
// error handling
return // or continue, etc.
}
// normal code
While either will work, as a matter of style, remove the unnecessary else and indentation. It's a similar to error flow indentation.
add a comment |
The Go Programming Language Specification
Return statements
A "return" statement in a function F terminates the execution of F,
and optionally provides one or more result values. Any functions
deferred by F are executed before F returns to its caller.
The specification defines the language. return terminates the function.
Go Code Review Comments
Indent Error Flow
Try to keep the normal code path at a minimal indentation, and indent
the error handling, dealing with it first. This improves the
readability of the code by permitting visually scanning the normal
path quickly. For instance, don't write:
if err != nil {
// error handling
} else {
// normal code
}
Instead, write:
if err != nil {
// error handling
return // or continue, etc.
}
// normal code
While either will work, as a matter of style, remove the unnecessary else and indentation. It's a similar to error flow indentation.
The Go Programming Language Specification
Return statements
A "return" statement in a function F terminates the execution of F,
and optionally provides one or more result values. Any functions
deferred by F are executed before F returns to its caller.
The specification defines the language. return terminates the function.
Go Code Review Comments
Indent Error Flow
Try to keep the normal code path at a minimal indentation, and indent
the error handling, dealing with it first. This improves the
readability of the code by permitting visually scanning the normal
path quickly. For instance, don't write:
if err != nil {
// error handling
} else {
// normal code
}
Instead, write:
if err != nil {
// error handling
return // or continue, etc.
}
// normal code
While either will work, as a matter of style, remove the unnecessary else and indentation. It's a similar to error flow indentation.
answered Nov 29 '18 at 10:21
peterSOpeterSO
98.8k16168183
98.8k16168183
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53530536%2fhow-return-is-working-in-this-tour-of-go-example%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown