Hello,
If we are trading links, allow me to quote myself.
http://dave.cheney.net/2012/01/18/why-go-gets-exceptions-right
I hope you do not dismiss Go so quickly. If you spend some time using the language, rather than being lead around by the great unwashed on reddit, you may have an opportunity to change your mind.
Dave
Its a shame that such a fantastic language is missing a better way to deal with errors.Exceptions might not be perfect, but are damn better than checking each and every function call.Or am I missing something?I just read a pair of posts that expresses these ideas much better than what I could here.As one of the comments I read said: 99% of the time I need to code the happy path and if something breaks in the middle the error handling is exactly the same: log the error, close resources, send the caller an error code or message, end of the story. I don't want to check every freaking single function invocation for errors.Its a shame, I really wanted to use go.--
Its a shame that such a fantastic language is missing a better way to deal with errors.Exceptions might not be perfect, but are damn better than checking each and every function call.Or am I missing something?I just read a pair of posts that expresses these ideas much better than what I could here.
As one of the comments I read said: 99% of the time I need to code the happy path and if something breaks in the middle the error handling is exactly the same: log the error, close resources, send the caller an error code or message, end of the story. I don't want to check every freaking single function invocation for errors.
Its a shame, I really wanted to use go.
--
Its a shame that such a fantastic language is missing a better way to deal with errors.Exceptions might not be perfect, but are damn better than checking each and every function call.
Or am I missing something?I just read a pair of posts that expresses these ideas much better than what I could here.As one of the comments I read said: 99% of the time I need to code the happy path and if something breaks in the middle the error handling is exactly the same: log the error, close resources, send the caller an error code or message, end of the story. I don't want to check every freaking single function invocation for errors.Its a shame, I really wanted to use go.
Rémy.
--
@DisposaBoy I do have my opinions, I just pointed to a blog that summed it up quite nicely. If you tell me to jump of a bridge, I will probably push you off instead. You are the one spamming the list with your reply devoid of content. As you can see, there has been quite some interest in my sololoquy.
--
--
Ian
--
@bryanturley Well, thats exactly the problem. If you fail to check it and something breaks in 2 days, you won't have a clue of what happened. With exceptions, you will know exactly what happened and exactly where and when.
On Tuesday, September 25, 2012 11:10:09 AM UTC-5, Larry Clapp wrote:
It's funny. When I heard about Go, I went to golang.org and read through the tour, and then went through it again and coded a lot of the problems in it. And you know what: I decided I like Go. How did you form your opinion about it?
I do like go. I also read the tour and bought a book.
Tangentially, how much Go have you written?
Very, very little. I made this post while fiddling with a piece of code that dind't do anything. Nothing got printed, no error reported, nada. How on earth am I supposed to start troubleshooting the damn thing. With exceptions, I will have a nice stacktrace that will pinpoint exactly where I messed up.
If you don't like Go, great. More power to you. What was your motivation for telling us so, and (in my opinion) so rudely, at that? Why should we respond with anything more than a crisp, clear, "fuck you too"?
Sorry for the rude post, I have already aknowledged that. It was also on purpose, because I knew it would get more attention...
-- Larry
On Monday, September 24, 2012 11:55:57 AM UTC-4, (unknown) wrote:Its a shame that such a fantastic language is missing a better way to deal with errors.Exceptions might not be perfect, but are damn better than checking each and every function call.Or am I missing something?I just read a pair of posts that expresses these ideas much better than what I could here.As one of the comments I read said: 99% of the time I need to code the happy path and if something breaks in the middle the error handling is exactly the same: log the error, close resources, send the caller an error code or message, end of the story. I don't want to check every freaking single function invocation for errors.Its a shame, I really wanted to use go.
--
On Tuesday, September 25, 2012 11:10:09 AM UTC-5, Larry Clapp wrote:
Tangentially, how much Go have you written?
It was also on purpose, because I knew it would get more attention...
On Tuesday, September 25, 2012 11:10:09 AM UTC-5, Larry Clapp wrote:
It's funny. When I heard about Go, I went to golang.org and read through the tour, and then went through it again and coded a lot of the problems in it. And you know what: I decided I like Go. How did you form your opinion about it?
I do like go. I also read the tour and bought a book.
Tangentially, how much Go have you written?
Very, very little. I made this post while fiddling with a piece of code that dind't do anything. Nothing got printed, no error reported, nada. How on earth am I supposed to start troubleshooting the damn thing. With exceptions, I will have a nice stacktrace that will pinpoint exactly where I messed up.
If you don't like Go, great. More power to you. What was your motivation for telling us so, and (in my opinion) so rudely, at that? Why should we respond with anything more than a crisp, clear, "fuck you too"?
Sorry for the rude post, I have already aknowledged that. It was also on purpose, because I knew it would get more attention...
-- Larry
--
--
The problem I have with all of this syntax changing magic is that it makes it harder to see that the error actually is being handled. With the full "if err != nil { }" form, it is clear that the possibility of error is considered. However with the proposed "n, return = writer.Write(buf)" and $ styles, it's easy to miss the handling. A drastic change in control flow like returning should always stand out in its own statement.
Thanks,
Josh
--
--
Ok, here is a dumbded down example of what I mean:package mainimport ("bufio""os")func main() {file, _ := os.OpenFile("/nowhere/", os.O_WRONLY|os.O_CREATE, 0666)writer := bufio.NewWriter(file);writer.WriteString("foobar\n")}Run this, and what will you get? Nothing. No error, no stacktrace ... absolutely nothing. Nada.I know that programers should check the error codes and should be super smart, and should have tests for their code, and should not have deadlines, and should be handsome ... but in reality, well you have been there.This applies to any error handling mechanism error codes, exceptions or you name it... as someone mentioned Go (or any language) will not do the error checking, the programmer will.So when the programmer, god forbid, fails to do his job I would rather have a180 line stacktrace pinpointing me where the stupid programmer messed up.
On Monday, September 24, 2012 10:55:57 AM UTC-5, (unknown) wrote:
Its a shame that such a fantastic language is missing a better way to deal with errors.Exceptions might not be perfect, but are damn better than checking each and every function call.Or am I missing something?I just read a pair of posts that expresses these ideas much better than what I could here.
As one of the comments I read said: 99% of the time I need to code the happy path and if something breaks in the middle the error handling is exactly the same: log the error, close resources, send the caller an error code or message, end of the story. I don't want to check every freaking single function invocation for errors.Its a shame, I really wanted to use go.
--
Do we really want people to adopt Go who are so against a fundamental
part of the language? I think Go is popular enough now where the Go
community shouldn't have to convince people to try it.
It's great if you think Python|Ruby|C++|etc is better than Go. Write
your code in your preferred language, solve real world problems, and
be happy. Think of all the time we're all wasting reading and
replying to this thread rather than improving software.
import (
"os"
)
func main() {
file := "/nowhere/"
text := "Prone to error"
print(writeToFile(file, text), "\n")
file = "here.txt"
text = "This might just work"
print(writeToFile(file, text), "\n")
}
// writeToFile takes two string arguments
// a file name and a string to write to that file
// and returns an error string for the write operation
func writeToFile(f, s string) (expl string) {
var e int
if file, err := os.OpenFile(f, os.O_RDWR|os.O_CREATE, 0666); err == nil {
if _, err := file.WriteString(s); err == nil {
e = 0
} else {
e = 1
}
} else {
e = 99
}
return explainError(e) + f
}
// explainError takes the file name and the error index
// and returns an explicit error text
func explainError(e int) (s string) {
switch e {
case 0:
s = "Successful write to file: "
case 1:
s = "There was an error writing to file: "
case 99:
s = "You got the file name (and/or its path) wrong: "
}
return s
So when the programmer, god forbid, fails to do his job I would rather have a180 line stacktrace pinpointing me where the stupid programmer messed up.
Not really. The problem with your "suggestion" is that it would be caught at compile time (which is good) but the lazy programmer will still just do _,_ := x() just to get past it.The real problem is at runtime, where you would be completely lost as to where the problem is.Of course the same goes for exceptions. The lazy programmer could simply catch and ignore the exception...(that is also why I favor unchecked exceptions... but that is another matter).Thanks to all for your answers.
--
Not really. The problem with your "suggestion" is that it would be caught at compile time (which is good) but the lazy programmer will still just do _,_ := x() just to get past it.
The real problem is at runtime, where you would be completely lost as to where the problem is.Of course the same goes for exceptions. The lazy programmer could simply catch and ignore the exception...(that is also why I favor unchecked exceptions... but that is another matter).
I very often see, `_, err := something()`, but I've yet to see an experienced go programmer do `x, _ := something()`. What that tells me is the error is the last thing that a normal go programmer will ignore.
--
Also I'm saying that behavior can be explicitly overridden in a way that you could read and identify fairly quickly reusing the token that currently means "ignore" for that purpose.
It almost feels consistent enough to be reasonable. I question the overall value of it though.
I wonder how it would impact the existing corpus of go 1.0+ code.
...which leaves open a long list of options, like:func (name *type) example(arg int) (int, float64, error) { }anda, b := example(1)where the compiler knows that there is an error returned and that it has special powers, as in the above where the implication could be there is an unchecked error here, so I will:1. refuse to compile2. print a stack trace when error is non-nill. ;-)3. only compile if the function making the call ALSO has an error return and when error comes back non-nill, automatically return from the function and pass that error as the function's error value. (implicit assignment of invisible return value and invisible "if not ok return error") in such cases. Rob might puke all over this, but it is one of the possibles. Think of this as a soft exception. It only means that errors are always unpacked from the return and examined, making ",ok" universal even when uncoded by the developer.
Ok, here is a dumbded down example of what I mean:
package mainimport ("bufio""os")func main() {file, _ := os.OpenFile("/nowhere/", os.O_WRONLY|os.O_CREATE, 0666)writer := bufio.NewWriter(file);writer.WriteString("foobar\n")}Run this, and what will you get? Nothing. No error, no stacktrace ... absolutely nothing. Nada.I know that programers should check the error codes and should be super smart, and should have tests for their code, and should not have deadlines, and should be handsome ... but in reality, well you have been there.This applies to any error handling mechanism error codes, exceptions or you name it... as someone mentioned Go (or any language) will not do the error checking, the programmer will.
So when the programmer, god forbid, fails to do his job I would rather have a180 line stacktrace pinpointing me where the stupid programmer messed up.
n := fmt.Println("Hello, playground")
prog.go:7: multiple-value fmt.Println() in single-value context
Are you suggesting a function definition flag that says "mr. Compiler, please error if my return values are ignored."?
Something like if func is capitalized?
This won't return any errors, but it doesn't mean all went according to the programmer's plan:
strings.Replace("Hello World", "l", "z", 20)
"Run this, and what will you get? Nothing. No error, no stacktrace ... absolutely nothing. Nada."
This means that I can safely conclude now that Go Error Handling Sucks ?
:)
String | replace (char oldChar, char newChar) Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar . |
String | replaceAll ( String regex, String replacement) Replaces each substring of this string that matches the given regular expression with the given replacement. |
String | replaceFirst ( String regex, String replacement) Replaces the first substring of this string that matches the given regular expression with the given replacement. |
func Replace(s, old, new string, n int) string
Replace returns a copy of the string s with the first n non-overlapping instances of old replaced by new. If n < 0, there is no limit on the number of replacements.
It can't get simpler or clearer than that.
Which takes us to Java String replace Sucks:
String
replace (char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences ofoldChar
in this string withnewChar
.String
replaceAll ( String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.String
replaceFirst ( String regex, String replacement)
Replaces the first substring of this string that matches the given regular expression with the given replacement.
What? No overloading?
Meanwhile, in Go:func Replace
func Replace(s, old, new string, n int) string
Replace returns a copy of the string s with the first n non-overlapping instances of old replaced by new. If n < 0, there is no limit on the number of replacements.
It can't get simpler or clearer than that.
--
.-'\
.-' `/\
.-' `/\
\ `/\
\ `/\
\ _- `/\ _.--.
\ _- `/`-..--\ )
\ _- `,',' / ,')
`-_ - ` -- ~ ,','
`- ,','
\,--. ____==-~
\ \_-~\
`_-~_.-'
\-~
--
Aram Hăvărneanu