Re: [go-nuts] Newbie question

2023-01-10 Thread Daniel Jankins
Thanks!

On Tue, Jan 10, 2023, 10:31 AM wagner riffel  wrote:

> On 1/10/23 11:42, Daniel Jankins wrote:
> > Hi,
> >
> > Why can you update a value in a map struct?
> >
>
> Sort answer, because m["foo"] is not addressable, but you can have the
> desired behavior using a temp variable:
>
> func f() {
> m := make(map[string]struct{ i int })
> x := m["foo"]
> x.i = 42
> m["foo"] = x
> }
>
> BR.
> -- w
>
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CACHdq70GYSHQTVVAe5FwWiURxwyBrqdQDdw9HPK8BT36z-R9UA%40mail.gmail.com.


Re: [go-nuts] Newbie question

2023-01-10 Thread 'wagner riffel' via golang-nuts

On 1/10/23 11:42, Daniel Jankins wrote:

Hi,

Why can you update a value in a map struct?



Sort answer, because m["foo"] is not addressable, but you can have the 
desired behavior using a temp variable:


func f() {
m := make(map[string]struct{ i int })
x := m["foo"]
x.i = 42
m["foo"] = x
}

BR.
-- w

--
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/dba18fd2-55cc-148b-48b2-6ae8ba90aa29%40104d.net.


[go-nuts] Newbie question

2023-01-10 Thread Daniel Jankins
Hi,

Why can you update a value in a map struct?

 // UnaddressableFieldAssign occurs when trying to assign to a struct field

// in a map value.
//
// Example:
//  func f() {
//  m := make(map[string]struct{i int})
//  m["foo"].i = 42
//  }

I am reading a yaml file and want to change some values in the struct.
Thank s
-- 
DanJ

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CACHdq72hMxFUv71AB9YGNx%3DwjUWxyoth2%3DoxbzxiQhyvf1mnzg%40mail.gmail.com.


Re: [go-nuts] Newbie question about type-casts

2020-08-04 Thread peterGo
Martin Møller Skarbiniks Pedersen,

Use a RoundUp function for n / d. For example,

// n / d rounded up
func RoundUp(n, d int64) int64 {
if d == 0 {
return 0
}
return (n + (d - 1)) / d
}

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

Peter

On Tuesday, August 4, 2020 at 6:37:06 AM UTC-4, Martin Møller Skarbiniks 
Pedersen wrote:
>
> On Sun, Aug 2, 2020 at 2:09 PM Martin Møller Skarbiniks Pedersen <
>> traxp...@gmail.com> wrote:
>>
>>> I have written my first little piece of Go-code.
>>> However it took some time and code for me to divide a int64 by 4 and 
>>> round down. 
>>>
>>> [...]
>>> var bet int64
>>> bet = int64(math.Ceil(float64(cash)/float64(4)))
>>>
>>>  
> On Sunday, 2 August 2020 23:25:23 UTC+2, Kurtis Rader wrote:
>>
>> In addition to Jake's question regarding whether you want to round up or 
>> down I'll point out there isn't any to cast to float64 and back to int64. 
>> If you want to "round down" just divide by four. If you want to "round up" 
>> add one less than the divisor before dividing; e.g., bet := (cash + 3) / 
>> 4. Notice the ":=" which avoids the need for the "var bet int64" statement.
>>
>
>
> OK. Good idea. But what is I need to integer divide with a variable and 
> not the number 4?
>
> Cheers
> Martin
>
>  
>
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/47ff7c07-4974-4a33-87e5-73bcbc9409e9o%40googlegroups.com.


Re: [go-nuts] Newbie question about type-casts

2020-08-04 Thread Jesper Louis Andersen
On Tue, Aug 4, 2020 at 12:31 PM Martin Møller Skarbiniks Pedersen <
traxpla...@gmail.com> wrote:

bet := int64(math.Ceil(float64(cash)/float64(4)))
>
>
Programming languages must make a conscious choice here. Do we make type
casts explicit or implicit? Go opted for the explicit approach, probably
based on the experience of C, where type promotion follows certain implicit
rules.

The advantage of an explicit approach is that you remove certain classes of
subtle errors from programs, in a trade-off for more typing. You can
arrange your values such that they already have the correct type at times,
but otherwise you have to make the conversion yourself explicitly in the
program.

-- 
J.

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAGrdgiV%3DHb%2BaQEq%3D%3D-4Xbn20U962Ye%2BsebUgqbQRGVyiwx5bnw%40mail.gmail.com.


Re: [go-nuts] Newbie question about type-casts

2020-08-04 Thread Michael Jones
var cash, change, bet int64
:
bet = cash/4

// change is 0,1,2,3 meaning 0/4, 1/4, 2/4, 3/4 dineros or zlottys or
whatever
change = cash - 4*bet // or, change = cash%4

// choose one of these
if change>0{
  bet++ // a: round up means 1/4 and 2/4 and 3/4 go to 1
}
// ...or...
if change>=2{
  bet++ // b: round up means 2/4 and 3/4 go to 1
}

cash = cash - bet

once you understand the above compare to these versions:

bet = (cash+3)/4 // this is Kurtis's advice, mode (a)
cash = cash - bet
...or...
bet = (cash+2)/4 // this is mode (b)
cash = cash - bet

On Tue, Aug 4, 2020 at 3:37 AM Martin Møller Skarbiniks Pedersen <
traxpla...@gmail.com> wrote:

> On Sun, Aug 2, 2020 at 2:09 PM Martin Møller Skarbiniks Pedersen <
>> traxp...@gmail.com> wrote:
>>
>>> I have written my first little piece of Go-code.
>>> However it took some time and code for me to divide a int64 by 4 and
>>> round down.
>>>
>>> [...]
>>> var bet int64
>>> bet = int64(math.Ceil(float64(cash)/float64(4)))
>>>
>>>
> On Sunday, 2 August 2020 23:25:23 UTC+2, Kurtis Rader wrote:
>>
>> In addition to Jake's question regarding whether you want to round up or
>> down I'll point out there isn't any to cast to float64 and back to int64.
>> If you want to "round down" just divide by four. If you want to "round up"
>> add one less than the divisor before dividing; e.g., bet := (cash + 3) /
>> 4. Notice the ":=" which avoids the need for the "var bet int64" statement.
>>
>
>
> OK. Good idea. But what is I need to integer divide with a variable and
> not the number 4?
>
> Cheers
> Martin
>
>
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/025665ff-21eb-4139-93c6-57f1adfc5cc9o%40googlegroups.com
> 
> .
>


-- 

*Michael T. jonesmichael.jo...@gmail.com *

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CALoEmQy2mSaMnyrAjJvH3QQXONjSgOnRLKodN8moFord36U5iw%40mail.gmail.com.


Re: [go-nuts] Newbie question about type-casts

2020-08-04 Thread Martin Møller Skarbiniks Pedersen

>
> On Sun, Aug 2, 2020 at 2:09 PM Martin Møller Skarbiniks Pedersen <
> traxp...@gmail.com > wrote:
>
>> I have written my first little piece of Go-code.
>> However it took some time and code for me to divide a int64 by 4 and 
>> round down. 
>>
>> [...]
>> var bet int64
>> bet = int64(math.Ceil(float64(cash)/float64(4)))
>>
>>  
On Sunday, 2 August 2020 23:25:23 UTC+2, Kurtis Rader wrote:
>
> In addition to Jake's question regarding whether you want to round up or 
> down I'll point out there isn't any to cast to float64 and back to int64. 
> If you want to "round down" just divide by four. If you want to "round up" 
> add one less than the divisor before dividing; e.g., bet := (cash + 3) / 
> 4. Notice the ":=" which avoids the need for the "var bet int64" statement.
>


OK. Good idea. But what is I need to integer divide with a variable and not 
the number 4?

Cheers
Martin

 

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/025665ff-21eb-4139-93c6-57f1adfc5cc9o%40googlegroups.com.


Re: [go-nuts] Newbie question about type-casts

2020-08-04 Thread Martin Møller Skarbiniks Pedersen

>
> On Sun, Aug 2, 2020 at 2:09 PM Martin Møller Skarbiniks Pedersen <
> traxp...@gmail.com > wrote:
>
>> I have written my first little piece of Go-code.
>> However it took some time and code for me to divide a int64 by 4 and 
>> round down. 
>>
>> How can the last two lines be rewritten?
>>
>> Regards
>> Martin
>>
>> package main
>>
>> import (
>> "fmt"
>> "bufio"
>> "math"
>> )
>>
>>
>> var cash int64
>> scanner := bufio.NewScanner(os.Stdin)
>>
>> scanner.Scan()
>> cash,err = strconv.ParseInt(scanner.Text(), 10, 64)
>> if err != nil {
>> fmt.Println(err)
>> }
>>
>> var bet int64
>> bet = int64(math.Ceil(float64(cash)/float64(4)))
>> cash = cash - bet
>>
>> On Sunday, 2 August 2020 23:25:23 UTC+2, Kurtis Rader wrote:
>>>
>>> In addition to Jake's question regarding whether you want to round up or 
>>> down I'll point out there isn't any to cast to float64 and back to int64. 
>>> If you want to "round down" just divide by four. If you want to "round up" 
>>> add one less than the divisor before dividing; e.g., bet := (cash + 3) / 
>>> 4. Notice the ":=" which avoids the need for the "var bet int64" statement.
>>>
>>
Oops. I actually needed to round up so I think I still need the math.Ceil() 
function.

If I write like this:


import (
"fmt"
"bufio"
"math"
)

var cash int64
scanner := bufio.NewScanner(os.Stdin)

scanner.Scan()
cash,err = strconv.ParseInt(scanner.Text(), 10, 64)
if err != nil {
fmt.Println(err)
}

bet := math.Ceil(cash/4)

then the compiler complains:
./casino.go:36:24: cannot use cash / 4 (type int64) as type float64 in 
argument to math.Ceil

My only solution is a lot of type-casting:
bet := int64(math.Ceil(float64(cash)/float64(4)))

Is there a better way to divide by 4 and round up to nearest integer?

ps. The full code can be found at: https://pastebin.com/1j7NJF0k


Regards
Martin



-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/e1064f2e-1120-444f-a9d9-bb7881969e49o%40googlegroups.com.


Re: [go-nuts] Newbie question about type-casts

2020-08-02 Thread Kurtis Rader
In addition to Jake's question regarding whether you want to round up or
down I'll point out there isn't any to cast to float64 and back to int64.
If you want to "round down" just divide by four. If you want to "round up"
add one less than the divisor before dividing; e.g., bet := (cash + 3) /
4. Notice the ":=" which avoids the need for the "var bet int64" statement.

On Sun, Aug 2, 2020 at 2:09 PM Martin Møller Skarbiniks Pedersen <
traxpla...@gmail.com> wrote:

> I have written my first little piece of Go-code.
> However it took some time and code for me to divide a int64 by 4 and round
> down.
>
> How can the last two lines be rewritten?
>
> Regards
> Martin
>
> package main
>
> import (
> "fmt"
> "bufio"
> "math"
> )
>
>
> var cash int64
> scanner := bufio.NewScanner(os.Stdin)
>
> scanner.Scan()
> cash,err = strconv.ParseInt(scanner.Text(), 10, 64)
> if err != nil {
> fmt.Println(err)
> }
>
> var bet int64
> bet = int64(math.Ceil(float64(cash)/float64(4)))
> cash = cash - bet
>
>
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/4aa04075-b8bc-41b1-bbb5-d11403e24763o%40googlegroups.com
> 
> .
>


-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD8rr%3Df5C%3DM3c5PueYQKBeT%2B23%2BxJrzxf_-FZyCgep584A%40mail.gmail.com.


[go-nuts] Newbie question about type-casts

2020-08-02 Thread Martin Møller Skarbiniks Pedersen
I have written my first little piece of Go-code.
However it took some time and code for me to divide a int64 by 4 and round 
down. 

How can the last two lines be rewritten?

Regards
Martin

package main

import (
"fmt"
"bufio"
"math"
)


var cash int64
scanner := bufio.NewScanner(os.Stdin)

scanner.Scan()
cash,err = strconv.ParseInt(scanner.Text(), 10, 64)
if err != nil {
fmt.Println(err)
}

var bet int64
bet = int64(math.Ceil(float64(cash)/float64(4)))
cash = cash - bet



-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/4aa04075-b8bc-41b1-bbb5-d11403e24763o%40googlegroups.com.


Re: [go-nuts] Newbie question on slice bound checking

2019-06-16 Thread Ugorji Nwoke
Thanks much, to you and Jan Merci.

I needed it to validate whether the performance gap in my library was due 
to bounds checking or not. If it was, then I would try harder to use tips 
to reduce bounds checking. I wasn't planning on running it in production.

I ended up having to use -gcflags=all=-B (previously I was only using 
-gcflags=-B and I didn't see any improved performance for any packages).

On Sunday, June 16, 2019 at 9:34:17 AM UTC-4, Michael Jones wrote:
>
> It has been, yes, and unless it changed in the last few weeks, it still 
> is. 
>
> I use to to measure cost as an awareness metric, but seldom run that way. 
> Go is safe until you do that, then it becomes unsafe. Too risky for serious 
> use. 
>
> On Sun, Jun 16, 2019 at 4:53 AM Ugorji Nwoke  > wrote:
>
>> I know this is an old thread, but is -B still supported?
>>
>>
>> On Monday, August 27, 2012 at 2:09:19 AM UTC-4, minux wrote:
>>>
>>>
>>> On Monday, August 27, 2012, bluehorn wrote:

 Is "-gcflags -B" still supported in go1.0.2?  I suppose it was gone 
 already.

>>> It is still there. 
>>>
>> -- 
>> 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 golan...@googlegroups.com .
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/397c0824-b5b5-412d-974e-b53f56d29a82%40googlegroups.com
>>  
>> 
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
> -- 
>
> *Michael T. jonesmichae...@gmail.com *
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/be4bb380-9e4d-45c4-a74e-7bce5b27f523%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Newbie question on slice bound checking

2019-06-16 Thread Jan Mercl
On Sun, Jun 16, 2019, 13:53 Ugorji Nwoke  wrote:

> I know this is an old thread, but is -B still supported?
>

That compiler flag provides compiling a program, semantic of which isn't
compatible with the Go language specification.

It's only valid use is to enable easy evaluation of the bounds checking
costs.

That said, I don't think the term "supported" applies. It's just a compiler
developer's tool.

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAA40n-UGio_%3Da36R06BXW286BMFj5yVToA65LNVR-F8gJMnbtw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Newbie question on slice bound checking

2019-06-16 Thread Michael Jones
It has been, yes, and unless it changed in the last few weeks, it still is.

I use to to measure cost as an awareness metric, but seldom run that way.
Go is safe until you do that, then it becomes unsafe. Too risky for serious
use.

On Sun, Jun 16, 2019 at 4:53 AM Ugorji Nwoke  wrote:

> I know this is an old thread, but is -B still supported?
>
>
> On Monday, August 27, 2012 at 2:09:19 AM UTC-4, minux wrote:
>>
>>
>> On Monday, August 27, 2012, bluehorn wrote:
>>>
>>> Is "-gcflags -B" still supported in go1.0.2?  I suppose it was gone
>>> already.
>>>
>> It is still there.
>>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/397c0824-b5b5-412d-974e-b53f56d29a82%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>
-- 

*Michael T. jonesmichael.jo...@gmail.com *

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CALoEmQzeLSEOUFo9wYC52gFGyfv1rfmtUZm1PPHw-MMx1XtT_Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Newbie question on slice bound checking

2019-06-16 Thread Ugorji Nwoke
I know this is an old thread, but is -B still supported?

On Monday, August 27, 2012 at 2:09:19 AM UTC-4, minux wrote:
>
>
> On Monday, August 27, 2012, bluehorn wrote:
>>
>> Is "-gcflags -B" still supported in go1.0.2?  I suppose it was gone 
>> already.
>>
> It is still there. 
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/397c0824-b5b5-412d-974e-b53f56d29a82%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Newbie question about struct definition

2018-12-03 Thread Brian Hatfield
Hi Freddy!

Those are Struct Tags (see here for more )
and are a way to add some metadata to struct fields that can be accessed
via reflection. Lots of libraries use these to denote names or other notes
about each fields. Those libraries expect to have a "known" struct tag used
so they can parse it. You can see here for a list of "well known" struct
tags , but anyone
can implement any struct tag if they so choose, via reflection
.

Good luck!
Brian

On Mon, Dec 3, 2018 at 10:01 PM Freddy Martinez 
wrote:

> Hey guys, I’ve been learning Go for the lat couple of weeks but now that
> I’m looking to more advanced topics, I’ve seen things like
>
> type Person struct {
> FirstName string `db:"first_name"`
> LastName  string `db:"last_name"`
> Email string
> }
>
> What `db:”first_name”` means ?
>
> Regards
>
>
>
> =
> Freddy Martínez García
> Software Engineer
> B.S. Computer Science
> LinkedIn: https://ar.linkedin.com/in/freddy-martinez-garcia-47157259
>
>
> *“If you give someone a program, you will frustrate them for a day;if you
> teach them how to program, yo will frustrate them for a lifetime.”*
>
> *David Leinweber*
>
> --
> 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] Newbie question about struct definition

2018-12-03 Thread Freddy Martinez
Hey guys, I’ve been learning Go for the lat couple of weeks but now that
I’m looking to more advanced topics, I’ve seen things like

type Person struct {
FirstName string `db:"first_name"`
LastName  string `db:"last_name"`
Email string
}

What `db:”first_name”` means ?

Regards



=
Freddy Martínez García
Software Engineer
B.S. Computer Science
LinkedIn: https://ar.linkedin.com/in/freddy-martinez-garcia-47157259


*“If you give someone a program, you will frustrate them for a day;if you
teach them how to program, yo will frustrate them for a lifetime.”*

*David Leinweber*

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