[go-nuts] Re: Modeling domain errors

2023-04-21 Thread 'Torben Schinke' via golang-nuts
Also asked this on reddit and the feedback can be found here: 
https://www.reddit.com/r/golang/comments/12tsnhf/who_returns_specific_error_interfaces_to_model/

Torben Schinke schrieb am Mittwoch, 19. April 2023 um 12:01:33 UTC+2:

> We are a team of experienced developers and are currently discussing how 
> we may improve our modeling of expected domain errors in our layered 
> architecture. We are used to work with the idiomatic way of defining and 
> matching against our custom error types. We followed and understand the 
> design decisions of the error interface and its usage within the standard 
> library. However, we are yet dissatisfied with our developer experience due 
> to the limited expressiveness. For example, it is quite common to oversee a 
> mapping case of an infrastructure error into a domain error. It is clear, 
> that we try to document our "error sum type" but without bothering the 
> compiler, that is just not helpful enough. 
>
> So we are now considering to define custom error interfaces with marker 
> methods to represent a sealed set of domain errors. The intention is to 
> create the most type safe coupling for error handling, which the Go 
> compiler can guarantee - just like we do with regular parameter types. We 
> don't need (or want) covariant result types here. And by still using an 
> interface, we also avoid the interface-type-not-nil edge case. 
>
> *I'm wondering how others deal with this in practice. *
> *Are there any experience reports? *
>
> Example
> // CalculationError defines a sealed set of multiple domain error types
> type CalculationError interface{
> error
> failedCalculation()bool
> }
>
> //...
>
> // ApplyCalculation is more specific about the error and violates 
> intentionally idiomatic go but
> // we have a tight coupling anyway.
> func ApplyCalculation(p SomeDomainType)(SomeDomainResult, 
> CalculationError){
> //...
> }
>
> related discussions (excerpt) 
>
>- https://go.dev/blog/error-handling-and-go 
>- https://go.dev/blog/go1.13-errors 
>- https://github.com/golang/go/issues/19412#issuecomment-1464178165 
>- https://dave.cheney.net/2014/12/24/inspecting-errors 
>- 
>
> https://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully
> 
>- 
>https://blog.merovius.de/posts/2018-01-21-what_even_is_error_handling/
>- ...
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/5a3bb83f-bfe3-43b6-8336-ff0b067dadb2n%40googlegroups.com.


Re: [go-nuts] Confusing Time Issue

2023-04-21 Thread jlfo...@berkeley.edu
You're absolutely right, of course.

Jon

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/f5233c95-870a-4cf1-b20f-a124bdd63f95n%40googlegroups.com.


Re: [go-nuts] Confusing Time Issue

2023-04-21 Thread Ian Lance Taylor
On Fri, Apr 21, 2023 at 5:27 PM jlfo...@berkeley.edu
 wrote:
>
> Consider the program below.
>
> I want to compute the duration between a time that I put in the 
> "old_time_str" variable and the current time. If I put the current time in a 
> string, the program shows the correct duration. If I get the current time 
> using time.Now() then the duration shown is 7 hours too long (I'm in PDST). 
> I'd like both methods to return the correct duration. Let's presume that  
> both times are in the same time zone.
>
> What am I doing wrong?

It's timezones.  Your time strings don't have any timezone
information, and the default differs for time.Parse and time.Now.  You
will probably get the results you want if you change your calls to
time.Parse(t, s) to time.ParseInLocation(t, s, time.Local).  See
https://pkg.go.dev/time#Parse.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcUEEBtFS6UAW9ptZdwo2gTR9DSJ-w6BY%3DfE%2Bj8E2ixADA%40mail.gmail.com.


[go-nuts] Confusing Time Issue

2023-04-21 Thread jlfo...@berkeley.edu
Consider the program below.

I want to compute the duration between a time that I put in the 
"old_time_str" variable and the current time. If I put the current time in 
a string, the program shows the correct duration. If I get the current time 
using time.Now() then the duration shown is 7 hours too long (I'm in PDST). 
I'd like both methods to return the correct duration. Let's presume that  
both times are in the same time zone.

What am I doing wrong?

Cordially,
Jon Forrest


package main

import (
"fmt"
"time"
)

var old_time_str string = "Thu Apr 20 20:26:40 2023"
var new_time_str string = "Fri Apr 21 17:07:51 2023"
var time_format string = `Mon Jan 02 15:04:05 2006`

func main() {
var new_time_1 time.Time
var new_time_2 time.Time
var old_time time.Time
var diff time.Duration

old_time, _ = time.Parse(time_format, old_time_str)
new_time_1, _ = time.Parse(time_format, new_time_str)

fmt.Printf("Old time is %s\n", old_time.Format(time_format))
fmt.Printf("New time 1 is %s\n", new_time_1.Format(time_format))

diff = new_time_1.Sub(old_time)
fmt.Printf("diff 1: %s\n\n", diff.String())

new_time_2 = time.Now()
fmt.Printf("Old time is still %s\n", old_time.Format(time_format))
fmt.Printf("New time 2 is %s\n", new_time_2.Format(time_format))

diff = new_time_2.Sub(old_time)
fmt.Printf("diff 2: %s\n", diff.String())
}

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/38c5c578-f540-4ad4-8b8d-9f5f85ea5ef1n%40googlegroups.com.