Comments (3)
@arthurkushman here's some of my thoughts, hopefully they help.
In regards to error handling, you're really just going to have to return all errors as type error
, an additional value. For example:
import "errors"
...
func myfn(str string) (string, error) {
if str == "hello" {
return str, nil
}
return "", errors.New("Input to myfn(string) must be hello!")
}
Handling errors for inputs that are possibly not integers should probably be done like:
Ref: https://github.com/arthurkushman/pgo/blob/master/pfiles.go#L54-L56
if !okOff || !okLim {
errmsg := fmt.Sprintf("Error on passing params with wrong types to FileGetContents: offset %T and limit %T", args[1], args[2])
return nil, errors.New(errmsg)
}
Having a variable name matching the name of a datatype in any language leads to confusion, especially when it comes to new people maintaining your code later. I still recommend renaming it, because as-is it would only make detailed code documentation confusing or even confuse someone reading through the code that's fresh into the language coming from another language. It's regarded as bad style and this is just one of the reasons many languages wont even allow you to do this.
Whenever you run your printError()
function it still has very bad side effects: https://github.com/arthurkushman/pgo/blob/master/core.go#L26-L28
Consider that your application should never exit, if it's supposed to be a web server using the http library. If someone just forgets to handle some input somewhere and an error occurs in many of your functions, os.Exit(1)
will close the entire program and the developer of that web app is left with a down website where only 1 small feature may have just been broken for a night.
In PHP where it's fine for you to die()
whenever, because script execution runs only once per page load.
In contrast when you write web apps in Go, Ruby, Python, etc. you're making a daemon that continually runs. die()
are not good for these use cases. Any API or library assuming that it should be doing process level stuff that could immediately stop an application is really bad in these scenarios and should only return errors for the developer to handle in his own code.
from pgo.
Excellent, thank u for this check-list, I really appreciate your contribution for opening this issue.
Let's try to re-check:
-
If you're writing a web app in go, you don't want your route to panic the entire application. This results in the site is down. Error handling does not translate between php and go. You have to just handle your errors. - Done with
fmt.Printf()
andos.Exit(1)
-
You're not handling potential type errors that could arise if that isn't an integer.
Fixed - now it is checked with type int
- What if your file contents aren't even long enough for this?
Then an error will be returned and printed as stated in
func (b *Reader) Discard(n int) (discarded int, err error) {
description: // If Discard skips fewer than n bytes, it also returns an error.
- ... variable name is variable type, rename that var...
Done as it is logically correct to save from further misspellings, misuses etc
Thx again and hope someday u will use it in production env, but at this moment this repo doesn't even have semver for that. I will definitely push this work further and try to cover all cases, then release 0.x.x versions that still do not recommended for use in prod env, only for developers own risk (if they don't mind for some reason), as it is a common practice to create any software in the world (with 0 zeroed semVers etc), it seems there is no point for "but I wouldn't run any of this in production" comment.
from pgo.
I've done as much as I could while finding time between pjs, if there will be any proposals - will be glad to move forward with better dev practices. Thx
from pgo.
Related Issues (20)
- Change Date panic to return current std time to do not break server code and backward compatibility HOT 1
- As an additional op to date - quarter of year
- Refactor tests to asserts
- Implement unixMilli and unixMicro in date.go
- Add PriorityQueue implementation HOT 1
- Add getA/C/MTime for files impl
- Add Rand(min, max)
- Add md5/sha1/sha2 simplified as in php HOT 1
- Add Serialize/Unserialize methods based on gob
- Hmac hashing functions impl
- Change ioutil to os/io implementation HOT 1
- Change HTTPBuildQuery to accept []string HOT 1
- Add EqualSlices to compare two slices of any types HOT 1
- Generic functions to implement convenient range of types HOT 1
- ArrayMax, ArrayMin impls to provide an ability to get min/max values from slices HOT 1
- Add ArrayUnique impl HOT 1
- Add ArrayReverse HOT 1
- Implement ConcatFast that concatenates strings faster than strings.Builder
- Change deprecated rand.Seed to NewSource/New HOT 1
- Fix resp.Body.Close() with error check in defer HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pgo.