Home >Backend Development >Golang >Go Transformers
go-utils is a library that is aimed provide useful libraries in go to reduce the developer efforts on building stuffs and increasing
productivity.
Few functionalities are mentioned below.
The basic problem that this is trying to solve is how you want to run multiple tasks based on a request that you recieved.
Consider the following scenario.
As you can see all these process can result in an error. Inherently Golang is very verbose in error handling. Sometimes we don't want to see that redundant code.
Also this reduces the readability.
Now consider another scenario -
Once you handle the error for processA and you forgot for processB Golang wont throw a compile time error causing you to miss this case. A small miss can cause havoc. Though we are supposed to follow a lot of process before shipping to prod ask yourselves do you follow always or not?
To solve this I have developed an approach where you will be
more focussed on writing what matters and how easy would it be to look at a fn and understand what it does. This will also help in overcoming variable shadowing. Instances where we have multiple errors being assinged an error shadowing can occur and this can be bad. Following this pattern and right coding can help in avoiding such weird scenarios.
Examples -
func FooBar() error { req := struct{ isFoo bool isBar bool }{} ctx := context.TODO() err := processFoo(ctx, &req) if err != nil { return err } err = processBar(ctx, &req) if err != nil { return err } return nil }
func FooBar() error { req := struct{ isFoo bool isBar bool }{} ctx := context.TODO() runner := NewSimpleTaskRunner(ctx, req) _, err := runner. Then(processFoo). Then(processBar). Result() return err }
As you can observe how better the code is readable and executable. This thought process and framework can improve the readability of the code.
func FooBar() error { req := struct{ isFoo bool isBar bool }{} ctx := context.TODO() runner := NewSimpleTaskRunner(ctx, req) _, err := runner. Parallel(processFooParallel). Parallel(processBarParallel). Result() return err }
We all know the famous lambdas and arrow functions. Golang
inherently doesnt support the arrow syntax. It would be nice to have
that in golang. For now suppose we need to do some Map operation that
is were things get hard. Well you are in for a cool implementation
from me to solve that for you. After this below implementation I would
ask you to think a soln of your own how this would have been implemented.
func TestMapRunner(t *testing.T) { // Create a map with some values floatingStrings := []string{"0.1", "0.2", "22", "22.1"} res, err := NewTransformer[string, float64](floatingStrings). Map(MapIt[string, float64](func(item string) (float64, error) { return strconv.ParseFloat(item, 64) })). Map(MapIt[float64, float64](func(item float64) (float64, error) { return item * 10, nil })). Result() if err != nil { t.Errorf("Testcase failed with error : %v", err) return } // Output: [0.1 0.2 22 22.1] t.Logf("Result: %v", res) assert.ElementsMatch(t, []any{float64(1), float64(2), float64(220), float64(221)}, res) }
The above example is for a conversion of string to float64.
This will handle the errors for you if there are any. The only exception will be there can be runtime errors if there is any
Type Cast Issues So be careful with this. Try to write the testcases
which should avoid this issue.
An addition to the functionality is made now, filteration also works.
Happy time folks!!
func FooBar() error { req := struct{ isFoo bool isBar bool }{} ctx := context.TODO() err := processFoo(ctx, &req) if err != nil { return err } err = processBar(ctx, &req) if err != nil { return err } return nil }
go get -u github.com/mahadev-k/go-utils@v1.0.1
Add this to your go.mod.
Use it as done in the examples module.
func FooBar() error { req := struct{ isFoo bool isBar bool }{} ctx := context.TODO() runner := NewSimpleTaskRunner(ctx, req) _, err := runner. Then(processFoo). Then(processBar). Result() return err }
func FooBar() error { req := struct{ isFoo bool isBar bool }{} ctx := context.TODO() runner := NewSimpleTaskRunner(ctx, req) _, err := runner. Parallel(processFooParallel). Parallel(processBarParallel). Result() return err }
Checkout the repo:
Github: https://github.com/mahadev-k/go-utils
Considering you had read this through and liked it. If you would like to connect with me
Follow me on X - https://x.com/mahadev_k_
Linkedin - https://in.linkedin.com/in/mahadev-k-934520223
Do propose more ideas for contributions to this repo if interested.
Thank you ??
The above is the detailed content of Go Transformers. For more information, please follow other related articles on the PHP Chinese website!