[go-nuts] sync.Map for caching

2017-08-31 Thread bep
sync.Map in Go 1.9 is a little low on examples/doc in the wild, so I 
thought I should ask here.

The new type is promoted as a replacement for  RWMutex in mostly read use 
cases with stable keys. I assume that a typical in memory cache would fit 
that description.

With RWMutex, if the cost of creating the cache item is high, I would maybe 
do something ala:

mu.RLock()
// Check cache
mu.RUnclock()

// Return if found

// If Not found:
mu.Lock()
defer mu.Unlock()
// Double check cache, return if found
// Create item and put in cache




I don't see how the above can be written with a sync.Map without adding a 
mutex.

bep

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: sync.Map for caching

2017-08-31 Thread bep
Thanks, snmed,

I was looking at LoadOrStore, but I was assuming that any func would be 
executed in any case.

A more illustrative running sample of your first example:

https://play.golang.org/p/vjlVu1GnUQ

Thanks again!

bep

fredag 1. september 2017 08.24.10 UTC+2 skrev snmed følgende:
>
> Hi 
>
> Here are two examples how to achieve it: 
> https://play.golang.org/p/wAtyGMSw6g or 
> https://play.golang.org/p/EZWZUOpuwb
>
> First example: In best cases it executes the creator function once and 
> returns always the stored item, in bad cases it executes the creator 
> function multiple times but returns always the first
> stored item. 
>
> Second example uses a lock to create and store the item.
>
> If the creator function is expensive, then I would use the latter example 
> otherwise the first.
>
> Cheers snmed
>
>
> Am Freitag, 1. September 2017 00:18:35 UTC+2 schrieb bep:
>>
>> sync.Map in Go 1.9 is a little low on examples/doc in the wild, so I 
>> thought I should ask here.
>>
>> The new type is promoted as a replacement for  RWMutex in mostly read use 
>> cases with stable keys. I assume that a typical in memory cache would fit 
>> that description.
>>
>> With RWMutex, if the cost of creating the cache item is high, I would 
>> maybe do something ala:
>>
>> mu.RLock()
>> // Check cache
>> mu.RUnclock()
>>
>> // Return if found
>>
>> // If Not found:
>> mu.Lock()
>> defer mu.Unlock()
>> // Double check cache, return if found
>> // Create item and put in cache
>>
>>
>>
>>
>> I don't see how the above can be written with a sync.Map without adding a 
>> mutex.
>>
>> bep
>>
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Variable scope for text/template

2017-10-08 Thread bep
https://github.com/golang/go/issues/10608

bep


fredag 6. oktober 2017 07.22.49 UTC+2 skrev Hein Meling følgende:
>
> Hi all,
>
> Please see linked example code. I've been trying set a "global" variable 
> ($LastName in the example) for use inside a named template (phony in the 
> example), but the scoping rules for the template packages does not follow 
> common scoping rules typically used in other languages, including Go. And 
> it is not possible to pass more "arguments/pipelines" to the named 
> template. Any suggestions on how to accomplish something like this.
>
> https://play.golang.org/p/VjeNwr6RFe
>
> All the best,
> :) Hein
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Conditionally set go template variables

2016-07-20 Thread bep
No.

See https://github.com/golang/go/issues/10608

In Hugo I built a hackish workaround, see http://gohugo.io/extras/scratch/

bep

onsdag 20. juli 2016 20.56.47 UTC+2 skrev Tong Sun følgende:
>
> How to conditionally set go template variables? 
>
> I want the output of the following code 
> https://play.golang.org/p/6e14hS2u1r
>
>
>  {
>  sweaters := Inventory{"", 17}
>  tmpl, err := template.New("test").
>  Parse(`{{$material := "something"}}{{if .Material}} {{$material := 
> .Material}} {{end}}` +
>  "{{.Count}} items are made of {{$material}}.\n")
>  check(err)
>  err = tmpl.Execute(os.Stdout, sweaters)
>  check(err)
>  }
>  {
>  sweaters := Inventory{"wool", 17}
>  tmpl, err := template.New("test").
>  Parse(`{{$material := "something"}}{{if .Material}} {{$material := 
> .Material}} {{end}}` +
>  "{{.Count}} items are made of {{$material}}.\n")
>  check(err)
>  err = tmpl.Execute(os.Stdout, sweaters)
>  check(err)
>  }
>
>
>
> to be 
>
> 17 items are made of something.
> 17 items are made of wool.
>
>
>
> Using the exact same template. Is that possible? 
>
> Thanks
>
>
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] [ANN] gitmap: map filenames to last commit-info

2016-07-22 Thread bep
I wrote this as part of a feature in Hugo, the static site generator, to 
use the last updated timestamp from the Git repo as the page modification 
date. But then I thought it might be interesting to others (and myself), so 
I pulled the code into its own repo.

https://github.com/bep/gitmap

A fairly fast way to create a map from all the filenames to info objects 
for a given revision of a Git repo.


This library uses os/exec to talk to Git. There are faster ways to do this 
by using some Go Git-lib or C bindings, but that adds dependencies I really 
don't want or need.


If some git log kung fu master out there have suggestions for improvements, 
please open an issue or a PR.


bep

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Go 1.7 is released

2016-08-16 Thread bep
Congrats! Good stuff!

I wrote the benchmark below to compare two branches in Hugo, but ran a set 
after 1.7 upgrade:

4 random but fairly big Hugo sites, each built 6 times, rendered to memory:

   - go1.6.2 0.17-DEV 24.479727984s - All OK
   - go1.6.2 0.17-MULTILINGUAL 26.49965009s - All OK
   - go1.7 0.17-DEV 21.830323612s - All OK 
   - go1.7 0.17-MULTILINGUAL 23.164846844s - All OK

That should be a great 10%+ speedup, and that in an application I would 
already consider pretty fast.


https://github.com/bep/hugo-benchmark






tirsdag 16. august 2016 02.04.08 UTC+2 skrev chai2010 følgende:
>
> go1.7.windows-amd64.zip missing.
>
> 2016-08-16 7:28 GMT+08:00 Chris Broadfoot >
> :
>
>> Hello gophers,
>>
>> We just released Go 1.7.
>>
>> You can read the announcement blog post here:
>>   https://blog.golang.org/go1.7
>>
>> You can download binary and source distributions from our download page:
>>   https://golang.org/dl/
>>
>> To compile from source using a Git checkout, update to the release with 
>> "git checkout go1.7" and build as usual.
>>
>> To find out what has changed, read the release notes:
>>   https://golang.org/doc/go1.7
>>
>> Thanks to everyone who contributed to the release.
>>
>> Chris
>>
>> -- 
>> 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...@googlegroups.com .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> -- 
> https://github.com/golang-china/gopl-zh
> https://github.com/golang-china
> https://github.com/chai2010
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Remove vs Delete

2016-08-19 Thread bep
I had this discussion with another Gopher about the difference between 
Delete and Remove.

I found this definition which I agree on:

*"Delete <http://dictionary.reference.com/browse/delete>* and *remove 
<http://dictionary.reference.com/browse/remove>* are defined quite 
similarly, but the main difference between them is that *delete* means 
*erase* (i.e. rendered nonexistent or nonrecoverable), while *remove* 
connotes *take away and set aside* (but kept in existence)."


Translated to computer terms, remove would be to flag it/hide it, while 
delete would be to delete it from disk/erease it.

But then he mentioned:

https://golang.org/pkg/os/#Remove

I don't expect the os.Remove* funcs to ever be renamed, but wouldn't it be 
more precise if they were named os.Delete etc.?

bep

-- 
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.
For more options, visit https://groups.google.com/d/optout.