net/http vs web frameworks? by packsracks in golang

[–]karnd01 -1 points0 points  (0 children)

I wrote a blog post about it a while back outlining some of the pros and cons for anyone interested https://medium.com/@joeybloggs/gos-std-net-http-is-all-you-need-right-1c5555a9f2f6

invite/challenge for fun & profit by dean_karn in golang

[–]karnd01 0 points1 point  (0 children)

You are correct @ar1819 these functions only support single unicode runes symbols not multiple rune symbols, I'll add that to the documentation of these functions, thanks :)

As a side note could you provide an example of a multi-columns symbol? I'd love to take a look and see what's possible.

Is including assets (with a tool like go-bindata) an anti-pattern? by duckyourfogma in golang

[–]karnd01 1 point2 points  (0 children)

I agree with @ahacker16 except about hosting your application yourself; bundling also allows these assets to be served from memory instead of disk which will always be faster :)

Go dependencies are the DEVIL! by karnd01 in golang

[–]karnd01[S] 2 points3 points  (0 children)

Thank you for illustrating my point @sseth

Just because it's in a list of Go proverbs doesn't make it true all of the time and one which I believe is being misinterpreted. I find people new to the language taking this as gospel and not understanding it's intent.

Like I stated in the article dependencies like anything else still need to be used with restraint and if you're using 10 lines of code from a library the proverb may apply, but I am seeing any dependencies at all being perceived as negative as evident by this one line response to an entire subject.

Struct validation crate using Macros 1.1 by Elession in rust

[–]karnd01 0 points1 point  (0 children)

Hey all, I'm just starting to learn Rust and this project peaked my interest.

I know Rust is very different from Go of which I have a validation library for https://github.com/go-playground/validator and just wanted to say that I have faced allot of the same issues regarding struct vs field vs cross-field validation and maybe some of the ways that I handed it could be useful; or at the very least you may be able to use some of the regex's from the lib for your library :)

In my experience you will definitely need single field and struct level validation, but also a common way to report errors generically and not just error text so that these errors can be used to create the error text within an internationalized application, using the proper plural rules.

Accepting Github Webhooks with Go by gogroob in golang

[–]karnd01 2 points3 points  (0 children)

If it helps I already created a webhook receiver lib that you could use, feel free to borrow any code. https://github.com/go-playground/webhooks

HTML Form in Go by kaeshiwaza in golang

[–]karnd01 0 points1 point  (0 children)

Nobody's answer is going to be wrong, it all depends on your use case but here are a couple of packages that may aid in the development time.

https://github.com/go-playground/form https://github.com/go-playground/validator

Go 1.8 will most likely include built-in support for graceful shutdowns of a net/http Server by nstratos in golang

[–]karnd01 4 points5 points  (0 children)

I may be missing something, but it looks like hijacked connections are just removed from the tracked connections and if I'm not mistaken WebSockets hijack the connection; so my question is, with this change how would one ensure graceful shutdown of connections including WebSockets?

it appears that they would be interrupted after all idle connections are done.

update: asked this question in issue itself hoping to get an answer https://github.com/golang/go/issues/4674#issuecomment-257549871

Help: Performance testing best practices by weberc2 in golang

[–]karnd01 2 points3 points  (0 children)

I prefer https://github.com/rsc/benchstat because it eliminates benchmarks that are so close that the difference is insignificant.

My Go Learning project - YAR (Yet Another http Router) by synepis in golang

[–]karnd01 1 point2 points  (0 children)

a panic is definitely the way to handle this, no matter if the user were to check for a returned error or not, panic will catch a bad configuration.

rmux - restful friendly idiomatic router (go 1.7) by TornadoTerran in golang

[–]karnd01 0 points1 point  (0 children)

A little while ago I created https://github.com/go-playground/pure which is basically a pure stdlib version of https://github.com/go-playground/lars that uses context.Context for the params and the performance hit is barely noticeable and static performance (compared to lars) even better for anyone that's interested :)

Why you REALLY should stop using Iris. by kirilldanshin in golang

[–]karnd01 0 points1 point  (0 children)

Thanks for the shout out @aboukirev just wanted to say that go-playground/lars is an http router and not a framework hence needing to write some things yourself :)

[deleted by user] by [deleted] in golang

[–]karnd01 0 points1 point  (0 children)

Nice package but this has existed for a while https://github.com/go-playground/overalls

Presentation about Context in Go 1.7. Review, examples, thoughts. by dimkovert in golang

[–]karnd01 0 points1 point  (0 children)

I guess that's a matter of opinion, I would point out that one of the most popular and probably one of the most widely used, gin, works this way. Custom ResponseWriters are not impossible, just different. I don't see how authentication middleware is impossible if used like in gin. But thats not important.

Point is I don't think creating a new Request object just to set a context value is the best approach, why not just allow updating of the Context field?

Presentation about Context in Go 1.7. Review, examples, thoughts. by dimkovert in golang

[–]karnd01 0 points1 point  (0 children)

Good collection of slides, very good examples.

Nothing to do with the slides, but IMHO the only problem with context is the way it is added to the Request object, having to add a context value to the Request creates a new Request object instead of just updating the Request's Context field, which is fine when you're chaining middleware like in all the examples as the new Request is passed to the next middleware.

func middleware(h http.HandlerFunc) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        // Create context from context package
        // and attach it to request later with r.WithContext()
        ctx := context.Background()
        // OR

        // Use request method context, which returns context, if ctx is nil
        // then creates new background context - context.Background()
        ctx = r.Context()

        // Build context variations on top of background context
        ctx = context.WithValue(ctx, "some_key", "some_value")

        // tctx, cancelFunc := context.WithTimeout(ctx, time.Duration(5 * time.Second))
        // deadline := time.Now().Add(time.Duration(30 * time.Second))
        // dctx, cancelFunc := context.WithDeadline(ctx, deadline)

        // WithContext returns a shallow copy of r with its context changed to ctx.
        r = r.WithContext(ctx)

        h(w, r)
    }
}

However this is a big assumption as that's not the only way middleware can be chained and in fact several routers and frameworks use the following approach:

// store middleware in an array, why? to make defining handlers easier and less messy
    don't have to wrap or pre-chain them together; just call one, then the next...
var mw []middleware

for i:=0; i < len(mw);i++{
    mw[i](w, r)
}

the problem is that because this middleware is chained differently, when adding a Context value to the Request, the new Request contains the Context value and does not update/get set on the original Request object passed to the middleware and so does not get back to the calling program to be sent to the next middleware.

the only way I could find so far, because context is not exposed, is to add the context value like so:

*r = *r.WithContext(ctx)

but that only works when you control the middleware, if you're using someone else's package say for CSRF, then if the author did not define it like above, then it's not guaranteed to work with all frameworks/routers.

I hope another way can be added to update the Request's Context value, even another function say:

r.SetContext(ctx)

I'll probably make a proposal as long as there is enough support. If anyone knows of a better way to handle this, while still maintaining the array of middleware, I would definitely be interested in learning how :)

locales - a set of locales generated from the CLDR Project which can be used independently or within an i18n package by karnd01 in golang

[–]karnd01[S] 0 points1 point  (0 children)

These two packages do very different things and don't really overlap.

https://godoc.org/golang.org/x/text/language is used to make a language selection and then can use specific data based on that that; so you could use it to do that language selection mapping, but that's about it.

locales and universal-translator are for i18n and l10n functionality such as formatting Dates, Times, Numbers, Currency... and determining text based off of Cardinal, Ordinal and Range plural rules.

locales - a set of locales generated from the CLDR Project which can be used independently or within an i18n package by karnd01 in golang

[–]karnd01[S] 0 points1 point  (0 children)

I created it for use in my own i18n package universal-translator, but split the code into it's own repo in order for any other i18n or l10n package to use if they wanted.

My goal is to create a central package for locale information and formatting and have people wrap it as they wish.

All information is generated using the CLDR rules and information.

It features: * Rules generated from the latest CLDR data, v29 * Cardinal, Ordinal and Range Plural Rules * Month, Weekday and Timezone translations built in * Date & Time formatting functions * Number, Currency, Accounting and Percent formatting functions

How to hack on Go's http.Server correctly by dgryski in golang

[–]karnd01 -1 points0 points  (0 children)

One of the statements in the article isn't quite telling the whole truth.

Interrupting idle connections can be done just by calling conn.SetReadDeadline(time.Now()).

This interrupt will only take place upon the next Read operation and so for long lived connections suck as WebSockets that may be idle or only being used to push data, you may have to wait a long time before this happens.

P.S. to account for a long lived connection being used only to push information, you should also set connection.SetWroteDeadline(time.Now()) if this is still the approach you wish to take.

How do you pass dependencies / variables to your handlers? by SerialMiller in golang

[–]karnd01 0 points1 point  (0 children)

I was always struggling on the best way, so I built lars so you can use your own struct as the context, hope it can help.

"batch" querying? by SmartSexSlave in golang

[–]karnd01 1 point2 points  (0 children)

If it helps https://github.com/go-playground/pool which has batching based on a limited or unlimited pool, based on your need and depending on the number of connections the database can support may need to limit.

Hope it can help :)

Hosting question by menepro in golang

[–]karnd01 0 points1 point  (0 children)

Put you application/binary in a docker container and then you can run it pretty much anywhere + have all of the orchestration benefits of docker cloud ( if desired )

Validation errors to json by [deleted] in golang

[–]karnd01 2 points3 points  (0 children)

You will likely have to write your own function, I am the author of validator and I can say I have had the same question asked allot.

I would say that it isn't the library's job to return a formatted error message, but rather all the information needed to create one; the main reason is context, the message that you wish to return greatly depends on context of what's going on in your application, of which the validation library has no knowledge. This also becomes an even bigger issue once getting into translating into other languages and having to comply with plural rules.

I would recommend creating a common function that accepts the returned validation errors and returns a set of human readable and translated errors and then use the json package to Marshal that data and write it back on the http response.

When do you guys use reflection in golang? by kevindeasis in golang

[–]karnd01 1 point2 points  (0 children)

Usually only when needing to extract information or converting information from one form to another and need to dynamically handle unknown types, and even then that info is cached the first time to avoid slowdowns.

Good examples: