FAQ
Nice, I didn't realise you could declare anonymous interfaces like that,
thanks for the tip.
On Tuesday, 21 May 2013 16:34:07 UTC+1, Brad Fitzpatrick wrote:

I could have also just written:

if ln, ok := body.(interface { Len() int }); ok {
req.ContentLength = int64(ln.Len())
}

Without introducing Lenner.

But the code conservatively only wanted to allow those named types.




On Tue, May 21, 2013 at 8:21 AM, Ryan Slade <ryan...@gmail.com<javascript:>
wrote:
Hi

I was looking at this code:
(The if body != nil type switch)
https://code.google.com/p/go/source/browse/src/pkg/net/http/request.go#446

This code could be simplified if we had a "Lenner" interface:
type Lenner() interface {
Len() int
}

We could then simply have something like:
if body != nil {
if l, ok := body.(Lenner); ok {
req.ContentLength = int64(l.Len())
}
}

However, where would be the best place to define this interface? Is it
worth doing?

As a side effect, doing it this way appeared to give a slight performance
improvement.

I haven't looked further into the issue as there may have been a good
reason this wasn't done before, but I'm sure there may be other places in
the code where having a Lenner interface could at least remove some
repetitive code.

I'd love to hear if anyone thinks this is worthwhile doing before I
submit a code review.

Thanks
Ryan

--

---
You received this message because you are subscribed to the Google Groups
"golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to golang-dev+...@googlegroups.com <javascript:>.
For more options, visit https://groups.google.com/groups/opt_out.

--

---
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Discussion Posts

Previous

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 3 of 3 | next ›
Discussion Overview
groupgolang-dev @
categoriesgo
postedMay 21, '13 at 3:21p
activeMay 21, '13 at 3:41p
posts3
users2
websitegolang.org

2 users in discussion

Ryan Slade: 2 posts Brad Fitzpatrick: 1 post

People

Translate

site design / logo © 2021 Grokbase