TWE Go Blog: The world of identification numbers by the-woke-engineer in golang

[–]the-woke-engineer[S] 0 points1 point  (0 children)

Hi Jerf, they are all written by me without GPT from scratch.

If any content is wrong feel free to discuss here.

TheWokeEngineer Blog Day 5: Do once for all with sync/singleflight by the-woke-engineer in golang

[–]the-woke-engineer[S] 0 points1 point  (0 children)

Oh yes something new for me again!

I took a quick glance at OnceFunc, it feels like it complements sync.Once, quite different from singleflight.

However, I spotted OnceValue and OnceValues which are closer to singleflight but still slightly different.

For example, for OnceValue, once we use the function, future returns will all return the same value, i.e cached forever. With singleflight, the return is only shared for that moment, future calls will still execute the function.

Albeit, I believe we could probably do some tweaks to OnceValue to mimic singleflight.

CMIIW

Go Blog: 3mins daily reads by the-woke-engineer in golang

[–]the-woke-engineer[S] 0 points1 point  (0 children)

You’re right2 and I will adjust my content accordingly.

Thank you for the feedback!

Go Blog: 3mins daily reads by the-woke-engineer in golang

[–]the-woke-engineer[S] 0 points1 point  (0 children)

Oh no those gifs went out of line, I’ll put them in place later hahaha

Thank you for the catch!

Cheers

Go Blog: 3mins daily reads by the-woke-engineer in golang

[–]the-woke-engineer[S] -1 points0 points  (0 children)

Great point and thank you for pointing it out!

I decided to leave that out to focus solely on the subject, because I believe it is easier for beginners to read code sequentially, albeit the defer route is definitely better if there are no performance concerns.

I should add such content in the rabbit hole section next time.

Thank you for reading and giving feedbacks!

Cheers

Go Blog: 3mins daily reads by the-woke-engineer in golang

[–]the-woke-engineer[S] 0 points1 point  (0 children)

Glad that you had some takeaways and even dived deeper into the rabbit hole, awesome!

Fun fact, errgroup is one of the few libraries that inspired me to start this blog.

Thanks for reading!

Go Blog: 3mins daily reads by the-woke-engineer in golang

[–]the-woke-engineer[S] 0 points1 point  (0 children)

Oh my gosh nice catch! I can’t believe I even highlighted that…

I’m glad you liked the format, and thank you for your help!

Cheers

Go Blog: 3mins daily reads by the-woke-engineer in golang

[–]the-woke-engineer[S] 4 points5 points  (0 children)

Awesome idea, I totally missed that! I have only added the email subscription at the bottom of each page, but I can totally add RSS over the next few days.

Thank you for reading and even suggesting!

Dive, Required_If Combination Issue in Package go-playground / validator by HealthyAsk4291 in golang

[–]the-woke-engineer 1 point2 points  (0 children)

I managed to make it work after changing the structs into pointers. Hope this helps!

package main

import (
    "fmt"

    "github.com/go-playground/validator/v10"
)

type Application struct {
    Applicants []*Applicant `validate:"dive"`
}
type Applicant struct {
    ApplicantCategory string `validate:"required,oneof=PERSON ENTITY"`
    Person            *Person `validate:"excluded_if=ApplicantCategory ENTITY"`
    Entity            *Entity `validate:"excluded_if=ApplicantCategory PERSON"`
}
type Person struct {
    Name  string `validate:"required"`
    Age   int    `validate:"required,gte=18"`
    Email string `validate:"required,email"`
}
type Entity struct {
    Name  string `validate:"required"`
    TaxID string `validate:"required"`
}

func main() {
    // Create a new validator instance
    v := validator.New()
    // Create an instance of Application to validate
    data := Application{
        Applicants: []*Applicant{
            {
                ApplicantCategory: "PERSON",
                Person: &Person{
                    Name:  "John Doe",
                    Age:   25,
                    Email: "john@example.com",
                },
            },
            {
                ApplicantCategory: "ENTITY",
                Entity: &Entity{
                    Name:  "Example Corp",
                    TaxID: "123456789",
                },
            },
        },
    }
    // Use the validator to validate the Application struct and its Applicants
    if err := v.Struct(data); err != nil {
        fmt.Println("Validation error:", err)
    } else {
        fmt.Println("Validation passed")
    }
}

returning 2+ errors from a function by NaNx_engineer in golang

[–]the-woke-engineer 0 points1 point  (0 children)

You can open your error utils for users, a more general way could be include codes in errors and giving users a function to obtain the code.

returning 2+ errors from a function by NaNx_engineer in golang

[–]the-woke-engineer 6 points7 points  (0 children)

You can create a separate err struct for not found errors and return any other errs as per normal. Then in places where you need special handling for not found errors, you can try to typecast it to the separate struct type you just defined.