Luke Mauldin | 28 Mar 02:03 2012
Picon

Assign to String Pointer

If I have a struct as shown below that contains a string pointer, how do I assign or initialize the string pointer?


type foo struct {
strPtr *string
strVal string
}
_ = foo {strVal: "a"} //This works
_ = foo {strPtr: "b"} //This gives me the error: cannot use "b" (type string) as type *string in field value

I am having to use a string pointer type because my struct is being unmarshalled using encoding/json and if the incoming string is null, JSON requires a string pointer to correctly unmarshall the value.  I think that JSON is using reflection to populate the string pointer but I want to be able to set and manipulate the string pointer from regular Go code without using reflection.  Is this possible and if so, how?

Luke

David Symonds | 28 Mar 02:06 2012

Re: Assign to String Pointer

Use a helper function or variable:

s := "b"
_ = foo{strPtr: &s}

or

func sPtr(s string) *string { return &s }
_ = foo{strPtr: sPtr("b")}

Dave.

Evan Shaw | 28 Mar 02:05 2012
Picon

Re: Assign to String Pointer

On Wed, Mar 28, 2012 at 1:03 PM, Luke Mauldin <lukemauldin@...> wrote:
> If I have a struct as shown below that contains a string pointer, how do I
> assign or initialize the string pointer?

There's no way to do it in a single expression. You need to do something like:

str := "a"
_ = foo{strPtr: &str}

- Evan

Nigel Tao | 28 Mar 02:07 2012

Re: Re: reading keyboard state

On 28 March 2012 09:15, Eduard Castany <eduard.castany@...> wrote:
> Anyway it starts firing lots of KeyRelease and KeyPress events if the key is
> pressed for more than a second so I continue having the same problem..

Yeah, X11 does that. If you run the "xev" program, which is written in
C, you should see the same behavior.

If you search for [X11 auto repeat], you may find some ideas on how to
work around this.

Guillermo Estrada | 28 Mar 02:13 2012
Picon

Random Number Generation in a given range

Hi, I've been reading some posts but I still have the same question.


I need to generate a random number between a min and a max value of floats (Float64) for example:

random(-0.001, 0.001)

Is there any way pkg "math/rand" could do this using the Normal Distribution or such? Most Random generation on the pkg works in [0.0,1.0)
The closest I found is:

func (*Rand) NormFloat64

But I need a way to clamp that range into my own, any ideas? Thnx in advance!
Luke Mauldin | 28 Mar 02:14 2012
Picon

Re: Assign to String Pointer

Thank you both, your solutions work perfectly.

On Tuesday, March 27, 2012 7:05:49 PM UTC-5, Evan Shaw wrote:

On Wed, Mar 28, 2012 at 1:03 PM, Luke Mauldin wrote:
> If I have a struct as shown below that contains a string pointer, how do I
> assign or initialize the string pointer?

There's no way to do it in a single expression. You need to do something like:

str := "a"
_ = foo{strPtr: &str}

- Evan

Andrew Gerrand | 28 Mar 02:28 2012

Re: Random Number Generation in a given range

On 28 March 2012 11:13, Guillermo Estrada <phrozen10@...> wrote:
> Hi, I've been reading some posts but I still have the same question.
>
> I need to generate a random number between a min and a max value of floats
> (Float64) for example:
>
> random(-0.001, 0.001)
>
> Is there any way pkg "math/rand" could do this using the Normal Distribution
> or such? Most Random generation on the pkg works in [0.0,1.0)
> The closest I found is:
>
> func (*Rand) NormFloat64
>
> But I need a way to clamp that range into my own, any ideas? Thnx in
> advance!

A normal distribution has no upper or lower bound, so NormFloat64
might not be what you want.

This expression will give you a random float bounded to [-0.001,0.001):

    (rand.Float64() * 2 - 1) * 0.001

Andrew

Andrew Gerrand | 28 Mar 02:29 2012

Re: Bash completion for go tool.

On 28 March 2012 02:31,  <sdegutis@...> wrote:
> It's more complete, cleaner, and faster? Then yes, please share! And please
> submit it for Go1 before it's too late! :)

It's already too late.

Andrew

si guy | 28 Mar 02:36 2012
Picon

Random Number Generation in a given range

If you clamp a normal distribution then it isn't a normal distribution anymore... Are you saying that you
want [-0.001,0.001] to be within n sigma of a mean of zero?

If that's the case then 
MyVal := rand.NormFloat64() * ( 0.001 / n )

You can clamp it afterwards with a switch
switch {
   case MyVal > 0.001:
      MyVal = 0.001
   case MyVal < -0.001:
      MyVal = -0.001
}

But again, this won't be a normal distribution anymore...

Kyle Lemons | 28 Mar 02:42 2012
Picon

Re: Re: Slice - initial size and append

On Tue, Mar 27, 2012 at 2:55 PM, shogg <lars.heyden-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
That wasn't my point. My point was why can't you read something like this in the go specs/docs
Append, it's an inbuild not well documented function.
Arguably, the builtin/append documentation could be updated, but the implementation-definable scheme used by append is not appropriate for the spec, and may or may not be consistent across different versions of Go or even between gccgo and gc (though they are now, I believe). 


Gmane