Re: [go-nuts] Re: How to generate generic code with generate?

2017-01-23 Thread Konstantin Khomoutov
On Mon, 23 Jan 2017 21:39:39 -0800 (PST)
hui zhang  wrote:

> > With github.com/badgerodon/goreify you can do this:
> As this github auther says:
> I found gengen  after implementing
> most of this functionality. It follows a similar approach, though
> doesn't take it quite as far.
> 
> gengen seems better according to github star.
> I wonder if there are any better packages out there ,  have u reach
> on this?

Check out [1].

You can also look at [2] which implements a different approach to code
templating ([3] is an example of its usage).

Please note that judging project based on the quantity of "Github stars"
is uh... suboptimal (which is an euphemism for "idiotic").
Otherwise we'd all be writing for node.js ;-)

1. https://github.com/ncw/gotemplate
2. http://alikewise.com/gen/
3. https://github.com/deckarep/golang-set

-- 
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: How to generate generic code with generate?

2017-01-23 Thread hui zhang
As this github auther says:
I found gengen  after implementing most 
of this functionality. It follows a similar approach, though doesn't take 
it quite as far.

gengen seems better according to github star.
I wonder if there are any better packages out there ,  have u reach on this?


在 2017年1月23日星期一 UTC+8下午10:04:22,Caleb Doxsey写道:
>
> Hi,
>
> With github.com/badgerodon/goreify you can do this:
>
> //go:generate goreify examples/min.Min numeric
>
> // Min finds the minimum value in a slice
> func Min(args ...generics.T1) (r generics.T1) {
> if len(args) == 0 {
> panic("the minimum of nothing is undefined")
> }
>
> r = args[0]
> for i := 1; i < len(args); i++ {
> if generics.Less(r, args[i]) {
> r = args[i]
> }
> }
> return r
> }
>
>
> (replacing examples/min with your package name)
>
> This gives:
>
> // MinInt finds the minimum value in a slice
> func MinInt(args ...int) (r int) {
> if len(args) == 0 {
> panic("the minimum of nothing is undefined")
> }
>
> r = args[0]
> for i := 1; i < len(args); i++ {
> if r < args[i] {
> r = args[i]
> }
> }
> return r
> }
>
>
> There are other libraries out there that can do the same thing.
>
> On Monday, January 23, 2017 at 1:19:25 AM UTC-5, hui zhang wrote:
>>
>> Since go did not support generic yet ,
>> Generate give us a alternative choice .
>> However,  although I read the official document.  
>> I still don't know how to use go generate to generate code.
>> Could anyone show us an example ?
>>
>> Let's take   std::min in c++ us an example.
>> min(x,y)   // type could be int int32 int64 int8  uint   float??
>> min(x,y,z,a,b..)  //Variable-length Argument 
>>
>

-- 
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] How to use []interface{} in generic programing

2017-01-23 Thread Justin Israel
In past answers to similar questions, I have seen the stated answers being
that []Fight and []IReset are not the same type, and there is no support
for automatically converting from one to the other, and it will not treat
the slice of concrete types as the slice of IReset interfaces because of a
difference in memory layout.

Basically I think you would need to make([]IReset) and then type assert
when setting the fields:
https://play.golang.org/p/xceK0aoTvQ

Justin


On Tue, Jan 24, 2017 at 2:47 PM hui zhang  wrote:

> check code below, How to use []interface{} in generic programing?
>
> type IReset interface {
>Reset()
> }
> type Fight struct {
>hp,mp int
> }
> func (my *Fight) Reset () {
>my.hp = 0
>my.mp = 0
> }
> func reset1(x IReset){
>x.Reset()
> }
> func reset(x []IReset) {
>for i := range x {
>   x[i].Reset()
>}
> }
> func  Reset() {
>arr := make([]Fight,1)
>arr[0].hp = 100
>reset1(arr[0])//OK
>reset(arr)// can not use []Fight as []IReset
> }
>
>
>
> --
> 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.
>

-- 
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] Re: How to make an anonymous recursive function?

2017-01-23 Thread Frank Solomon
This approach makes "nested" recursion possible:

https://github.com/fbsolo/ProjectEuler/blob/master/main.go

- Frank

On Friday, April 25, 2014 at 7:24:28 AM UTC-7, emepyc wrote:
>
> Yes, but you should return the result ;-) 
>
> http://play.golang.org/p/h-VYDrY7Ov 
>
> M; 
>
> On 25/04/14 14:41, Francesc Campoy Flores wrote: 
> > And just because you should always use early return: 
> > 
> > http://play.golang.org/p/cbiM9icWr8 
> > 
> > 
> > On Fri, Apr 25, 2014 at 7:40 AM, Francesc Campoy Flores 
> > > 
> wrote: 
> > 
> > You could also pass the function to itself and define a recursive 
> > function type: 
> > 
> > http://play.golang.org/p/bYn0yGE-sL 
> > 
> > More for fun that for production though, this is not the most 
> > readable code ever. 
> > 
> > 
> > On Fri, Apr 25, 2014 at 6:04 AM,  > > wrote: 
> > 
> > I have a similar solution for a simple Fibonacci generator: 
> > 
> > func main() { 
> > var fibonacci func(int) int 
> > fibonacci = func (n int) int { 
> > if n == 0 { 
> > return 0 
> > } else if n == 1 { 
> > return 1 
> > } else { 
> > return fibonacci(n - 1) + fibonacci(n - 2) 
> > } 
> > } 
> > fmt.Println("Fibonacci (30) =>", fibonacci(30)) 
> > } 
> > 
> > This seems to be the most simple way to do a anonymous recursive 
> > function in Go. 
> > 
> > Regards, 
> > 
> > Anindya Chatterjee 
> > 
> > On Monday, November 16, 2009 4:18:43 PM UTC+5:30, Helmar wrote: 
> > 
> > It's not nice to always write something like: 
> > 
> > var q func(p [][]byte); 
> > q = func(p [][]byte) { 
> >... 
> >q(...); 
> >... 
> > }; 
> > 
> > Is there a better way? 
> > 
> > Regards, 
> > -Helmar 
> > 
> > -- 
> > 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. 
> > 
> > 
> > 
> > 
> > -- 
> > -- 
> > Francesc Campoy 
> > http://twitter.com/francesc  
> > 
> > 
> > 
> > 
> > -- 
> > -- 
> > Francesc Campoy 
> > http://twitter.com/francesc  
> > 
> > -- 
> > 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. 
>
>

-- 
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] net/rpc result types

2017-01-23 Thread Pablo Rozas Larraondo
Hi,

The rpc documentation says that a remote rpc method can be registered if
the second argument (response) is a pointer.

https://golang.org/pkg/net/rpc/#Server.Register

Does anyone know if there is a way of having an interface declared as this
response type and then assign pointers to it that satisfy that interface?

When I try to do it the compiler complains asking for a pointer type, but
considering an interface is already "kind" of a pointer* it seems to me it
can be possible. I guess it would be very useful to have generic remote
functions that are able to return any type that implements an interface,
the same way local functions do.

Thanks,
Pablo

*
http://stackoverflow.com/questions/13511203/why-cant-i-assign-a-struct-to-an-interface

-- 
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] Results of the 2016 Go User Survey?

2017-01-23 Thread faiface2202
Hi,

what is the state of 2016 Go User Survey? Are the results ever gonna be 
available? Or are they already and I've just missed them?

Thanks,
Michal Štrba

-- 
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] Why is passing a pointer to a pointer in cgo dissalowed?

2017-01-23 Thread Ian Lance Taylor
On Mon, Jan 23, 2017 at 3:57 AM,   wrote:
> The doc for cgo states ...
>
> "Go code may pass a Go pointer to C provided the Go memory to which it
> points does not contain any Go pointers. The C code must preserve this
> property: it must not store any Go pointers in Go memory, even temporarily.
> When passing a pointer to a field in a struct, the Go memory in question is
> the memory occupied by the field, not the entire struct. When passing a
> pointer to an element in an array or slice, the Go memory in question is the
> entire array or the entire backing array of the slice.
>
> Why is this? Is it because go only keeps track of the pointers that are
> direct arguments to a cgo call.  So if for instance a struct containing the
> only pointer to some value is passed to cgo then go loses track of that
> reference and the memory pointed at will be up for garbage collection?

Basically, yes.  See
https://github.com/golang/proposal/blob/master/design/12416-cgo-pointers.md
and https://golang.org/issue/12416.

Ian

-- 
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] Foregrounding, process management, os/exec.Cmd

2017-01-23 Thread James Aguilar
On Sun, Jan 22, 2017 at 8:45 PM Ian Lance Taylor  wrote:

> On Sat, Jan 21, 2017 at 4:07 PM, James Aguilar 
> wrote:
> > If you don't know or care about pagers and process management, you can
> stop
> > reading now.
> >
> > Background: I have a program that compile my code each time I modify it.
> It
> > produces logs continuously on a terminal.
> >
> > I'm trying to write a go program to wrap this program so that each
> compile
> > log is opened in a pager, and so that when a new compile starts, the
> pager
> > for the previous compile is automatically closed. Essentially, I'm
> trying to
> > change the program from doing (pseudo-shell)
> >
> > while true; do
> >   awaitChange
> >   build
> > end
> >
> > to:
> >
> > while true; do
> >   awaitChange
> >   kill $!
> >   build | less &  # <--- except less should be in the foreground of the
> > terminal
> >   to_kill=$!
> > end
> >
> > There are wrinkles: I don't control the program that executes this loop.
> So
> > I wrote a go program to process the input and separate it into a series
> of
> > output buffers based on a regexp. I've gotten it to the point where I can
> > start and kill less, and feed it the separated inputs.
> >
> > My problem: less is not behaving as it would if you ran it on the
> > commandline. The arrow keys don't work, ^C either does nothing (on the
> first
> > run of less) or interrupts the parent program, etc.
> >
> > I believe my mistake is that I am not correctly putting less into the
> > foreground. I also suspect that I'm not correctly moving my go program
> back
> > into the foreground each time I kill the child less process. I'm
> wondering
> > if anyone knows the magical incantation that is required to make this
> work
> > properly. My current code is here (with the compiler replaced by a random
> > number generator, since it is proprietary to the company I work for). I
> have
> > marked with TODOs the things I think are not working correctly.
> >
> > https://play.golang.org/p/Xv0y-Ln7aC
> >
> > Any ideas what I'm doing wrong here?
>
> Your program seems to work for me, so I guess I misunderstand what the
> problem is.
>
> I ran `go build foo.go; ./foo`.  It prints a series of random numbers
> from 0 to 99, fed into less.  When it prints a number 95 to 99, it
> restarts less.  When it prints enough numbers to fill my terminal, I
> see the `less` prompt (a colon).  I can use arrows, space, etc.
> Eventually that instance of less is killed and a new one starts.
> Hitting ^C just causes `less` to beep.  Hitting `q` causes less and
> the main program to exit.
>
> What do you see?  (Note that you shouldn't use `go run` for a program
> like this.)
>

Huh. It looks like it is working. The bug here is in my understanding of
less. I thought that I could interrupt and get a prompt when I was looking
at the unfinished part of a file. But it seems that doesn't work. So I
probably need to provide an EOF once I'm sure there will be no more output,
so the user can quit out from less if the build log is less than a full
page long.

There was also the issue of being able to interrupt the whole program with
^C during the second less and after. I believe that this was being caused
by a race between starting the second less exec.Cmd and the TIOCSPGRP ioctl
call at the end of the first one. The ioctl was stealing the foreground
from the running less program, because it ran after the command started.
Remove that call seems to fix the problem.

Thanks so much for your help!

-- James

-- 
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] How to dllexport a value symbol within a windows binary executable with Go?

2017-01-23 Thread Hakan Guleryuz
In normal C/C++ SDL2 related app I write, adding the following code

extern "C" {
   _declspec(dllexport) DWORD NvOptimusEnablement = 0x0001;
}

causes the driver system with integrated and dedicated nvidia gpu, to auto 
select the dedicated nvidia gpu. 

See here for some explanation of the mechanism->
https://github.com/urho3d/Urho3D/issues/139
http://stackoverflow.com/questions/16823372/forcing-machine-to-use-dedicated-graphics-card




I can view the exports of the final executable with "dumpbin /exports 
"

[dumpbin is a tool installed with visual studio C++ tools]


How can I make my go executable export this value symbol?

I searched go link documentation but could not find something useful to 
achieve this.

I did some tests with cgo and "//export" command with a simple var, but 
that did not have any effect on "dumpbin /exports" output (no symbols 
exported)


I can see which GPU the executable is using by inspecting it with "process 
explorer" and its "dedicated GPU memory usage"


The go build "go-sdl2" app, by default, uses the CPU's embedded low 
performance integrated GPU, since I can not export this value with the 
executable.


Thanks for any help.



-- 
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] Why is passing a pointer to a pointer in cgo dissalowed?

2017-01-23 Thread pierspowlesland
The doc for cgo states ...

"Go code may pass a Go pointer to C provided the Go memory to which it 
points does not contain any Go pointers. The C code must preserve this 
property: it must not store any Go pointers in Go memory, even temporarily. 
When passing a pointer to a field in a struct, the Go memory in question is 
the memory occupied by the field, not the entire struct. When passing a 
pointer to an element in an array or slice, the Go memory in question is 
the entire array or the entire backing array of the slice.

Why is this? Is it because go only keeps track of the pointers that are 
direct arguments to a cgo call.  So if for instance a struct containing the 
only pointer to some value is passed to cgo then go loses track of that 
reference and the memory pointed at will be up for garbage collection?

Thanks,

Piers



-- 
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] cross-compilation for go-gl applications

2017-01-23 Thread Aram Hăvărneanu
Those packages use cgo, so you will need a gcc-based toolchain for
your target platform. Either you will have to build it yourself
(doing that on Windows is probably challenging), or perhaps you can
find a prebuilt gcc (good luck with that). Make sure to install the
relevant OpenGL headers and libraries for your target and that your
cross-compiler can find them.

Then you can just do something like (substitute for your target):

CGO_ENABLED=1 CC=sparcv9-solaris2.12-gcc GOOS=solaris GOARCH=sparc64 go
build foo

Depending on a few other factors you might or might not need to
explicitely set internal or external linking.

CGO_ENABLED=1 CC=sparcv9-solaris2.12-gcc GOOS=solaris GOARCH=sparc64 go
build -ldflags='-linkmode=external' foo

If I were you, I would rather find a way to build natively.

-- 
Aram Hăvărneanu

-- 
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: How to generate generic code with generate?

2017-01-23 Thread Caleb Doxsey
Hi,

With github.com/badgerodon/goreify you can do this:

//go:generate goreify examples/min.Min numeric

// Min finds the minimum value in a slice
func Min(args ...generics.T1) (r generics.T1) {
if len(args) == 0 {
panic("the minimum of nothing is undefined")
}

r = args[0]
for i := 1; i < len(args); i++ {
if generics.Less(r, args[i]) {
r = args[i]
}
}
return r
}


(replacing examples/min with your package name)

This gives:

// MinInt finds the minimum value in a slice
func MinInt(args ...int) (r int) {
if len(args) == 0 {
panic("the minimum of nothing is undefined")
}

r = args[0]
for i := 1; i < len(args); i++ {
if r < args[i] {
r = args[i]
}
}
return r
}


There are other libraries out there that can do the same thing.

On Monday, January 23, 2017 at 1:19:25 AM UTC-5, hui zhang wrote:
>
> Since go did not support generic yet ,
> Generate give us a alternative choice .
> However,  although I read the official document.  
> I still don't know how to use go generate to generate code.
> Could anyone show us an example ?
>
> Let's take   std::min in c++ us an example.
> min(x,y)   // type could be int int32 int64 int8  uint   float??
> min(x,y,z,a,b..)  //Variable-length Argument 
>

-- 
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] Question about parallel tests

2017-01-23 Thread Diego Bernardes
If a test is marked as parallel, does the sub tests run in parallel too? Or 
should i mark then as parallel?

-- 
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] Foregrounding, process management, os/exec.Cmd

2017-01-23 Thread Peter Waller
I'm afraid I can't find the code, but I actually did this in the distant
past and recall having some success. (I was the one who introduced Ctty to
SysProcAttr for this very purpose! :).

One thing it seems as though you're missing is you don't set
SysProcAttr.Setctty. The documentation string for that field notes that it
won't have any effect unless you also use setsid.

type SysProcAttr struct {
...
Setctty  bool   // Set controlling terminal to fd Ctty
(only meaningful if Setsid is set)
...
}

Did you try these things?

I do remember reading about a lot of arcane behaviour in the manpages. See
the manpages for setsid(2), credentials(7), pty(7) and be sure to
understand them in as much detail as you can.

By the way, a neat hack for finding other people who have done similar
things:

https://github.com/search?l=Go=SysProcAttr+ctty=Code=%E2%9C%93


On 22 January 2017 at 00:07, James Aguilar  wrote:

> If you don't know or care about pagers and process management, you can
> stop reading now.
>
> *Background:* I have a program that compile my code each time I modify
> it. It produces logs continuously on a terminal.
>
> I'm trying to write a go program to wrap this program so that each compile
> log is opened in a pager, and so that when a new compile starts, the pager
> for the previous compile is automatically closed. Essentially, I'm trying
> to change the program from doing (pseudo-shell)
>
> while true; do
>   awaitChange
>   build
> end
>
> to:
>
> while true; do
>   awaitChange
>   kill $!
>   build | less &  # <--- except less should be in the foreground of the
> terminal
>   to_kill=$!
> end
>
> There are wrinkles: I don't control the program that executes this loop.
> So I wrote a go program to process the input and separate it into a series
> of output buffers based on a regexp. I've gotten it to the point where I
> can start and kill less, and feed it the separated inputs.
>
> *My problem:* less is not behaving as it would if you ran it on the
> commandline. The arrow keys don't work, ^C either does nothing (on the
> first run of less) or interrupts the parent program, etc.
>
> I believe my mistake is that I am not correctly putting less into the
> foreground. I also suspect that I'm not correctly moving my go program back
> into the foreground each time I kill the child less process. I'm wondering
> if anyone knows the magical incantation that is required to make this work
> properly. My current code is here (with the compiler replaced by a random
> number generator, since it is proprietary to the company I work for). I
> have marked with TODOs the things I think are not working correctly.
>
> https://play.golang.org/p/Xv0y-Ln7aC
>
> Any ideas what I'm doing wrong here?
>
> --
> 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.
>

-- 
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: [ANN] template-compiler: compile go templates to go code

2017-01-23 Thread Egon
Awesome :)

On Saturday, 21 January 2017 16:58:53 UTC+2, mhh...@gmail.com wrote:
>
> Hi,
>
> I have been working on a package to compile go templates to regular go 
> code.
> Today i can announce an early release of it.
>
> Find it here 
> https://github.com/mh-cbon/template-compiler
>
> There is still have a lot to test and comment, 
> and to detect some edge cases.
> But the general idea and structure exists, does work,
>  and shows a way to make that happen.
>
> If you are interested to get this production ready, 
> i d like very much to see your questions and PR!
>
> Being in a hurry, this announce is very short, 
> I expect a more detailed announce later.
>
> This said, i d like to ask the go team if the template package
> could have a new structure to register a func as a template,
> see 1 
> ,
>  
> 2 
> 
>  
> and 3 
> 
>
> Finally, i want to take chance of this message to wish
> to the golang, core team, contributors, users
> an happy new year and all the best for 2017!
>
> Happy coding!
>

-- 
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.