[go-nuts] Re: Map to struct and vice versa

2021-05-17 Thread Howard C. Shaw III
The examples given in other responses are great if you need to handle 
arbitrary or unknown maps and very much fit the 'easy way' part of your 
initial question. But you also asked at the end 'is there a more direct 
way?'.

If you actually know what you are getting, you can code it entirely 
directly:

t := T{
A: m["a"].(string),
B: m["b"].(int),
}

and back again

m2 := map[string]interface{}{}
m2["a"] = t.A
m2["b"] = t.B

https://play.golang.org/p/4OY3QoA5Mr3

Why would you not do this? Because your assignment will panic if you pass 
an m that is missing "a" or "b". Not fun. (Something like "panic: interface 
conversion: interface {} is nil, not int") Of course, you can always 
collect the values from the map into temporaries before creating the type 
and handle any nils in that process. Or pass your map through a validation 
step that ensures it is safe to convert before passing it to the conversion.

-- 
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/4a705e62-68f0-4136-804b-826eea48bc9dn%40googlegroups.com.


Re: [go-nuts] gonum plot : font error

2021-05-17 Thread Fred
Thanks Carla and Sébastien for your quick answer !

Carla your solution works, when I use modules I do not have font errors. 

What is strange is that I was not using modules in any computer to try this 
example and it was working in one an not the other :) 

Le lundi 17 mai 2021 à 15:10:55 UTC+2, Sebastien Binet a écrit :

> Salut Fred,
>
> On Mon May 17, 2021 at 14:11 CET, Fred wrote:
> > Hi,
> > I have 2 computers with Linux Manjaro and go version 1.16.3.
> > The same scatter plot
> >  works
> > on
> > one computer and produces on the other these already known errors :
> > 
> >
> > # gonum.org/v1/plot/text
> > /home/fred/GO/src/gonum.org/v1/plot/text/latex.go:153:3: cannot use
> > hdlr.Fonts.Lookup(rm, fnt.Size).Face (type *sfnt.Font) as type
> > *truetype.Font in field value
> > /home/fred/GO/src/gonum.org/v1/plot/text/latex.go:154:3: cannot use
> > hdlr.Fonts.Lookup(rm, fnt.Size).Face (type *sfnt.Font) as type
> > *truetype.Font in field value
> > /home/fred/GO/src/gonum.org/v1/plot/text/latex.go:155:3: cannot use
> > hdlr.Fonts.Lookup(it, fnt.Size).Face (type *sfnt.Font) as type
> > *truetype.Font in field value
> > /home/fred/GO/src/gonum.org/v1/plot/text/latex.go:156:3: cannot use
> > hdlr.Fonts.Lookup(bf, fnt.Size).Face (type *sfnt.Font) as type
> > *truetype.Font in field value
> > /home/fred/GO/src/gonum.org/v1/plot/text/latex.go:157:3: cannot use
> > hdlr.Fonts.Lookup(bfit, fnt.Size).Face (type *sfnt.Font) as type
> > *truetype.Font in field value
> > /home/fred/GO/src/gonum.org/v1/plot/text/latex.go:221:3: cannot use
> > op.Glyph.Font (type *truetype.Font) as type *sfnt.Font in field value
>
> this should indeed be fixed w/ gonum/pl...@v0.9.0.
>
> >
> > an update does not fix the error.
> >
> > go get -u gonum.org/v1/plot/...
> > # cd /home/fred/GO/src/github.com/go-latex/latex; git pull --ff-only
> > Votre information de configuration indique de fusionner avec la
> > référence
> > 'refs/heads/master'
> > du serveur distant, mais cette référence n'a pas été récupérée.
> > package github.com/go-latex/latex/drawtex: exit status 1
>
> this error may come from the fact that go-latex/latex is now using
> 'main' as its main branch in lieu of 'master'.
>
> hth,
> -s
>

-- 
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/efb3fb0c-cbeb-4815-a0d9-ee7a4b638ad8n%40googlegroups.com.


Re: [go-nuts] gonum plot : font error

2021-05-17 Thread 'Sebastien Binet' via golang-nuts
Salut Fred,

On Mon May 17, 2021 at 14:11 CET, Fred wrote:
> Hi,
> I have 2 computers with Linux Manjaro and go version 1.16.3.
> The same scatter plot
>  works
> on
> one computer and produces on the other these already known errors :
> 
>
> # gonum.org/v1/plot/text
> /home/fred/GO/src/gonum.org/v1/plot/text/latex.go:153:3: cannot use
> hdlr.Fonts.Lookup(rm, fnt.Size).Face (type *sfnt.Font) as type
> *truetype.Font in field value
> /home/fred/GO/src/gonum.org/v1/plot/text/latex.go:154:3: cannot use
> hdlr.Fonts.Lookup(rm, fnt.Size).Face (type *sfnt.Font) as type
> *truetype.Font in field value
> /home/fred/GO/src/gonum.org/v1/plot/text/latex.go:155:3: cannot use
> hdlr.Fonts.Lookup(it, fnt.Size).Face (type *sfnt.Font) as type
> *truetype.Font in field value
> /home/fred/GO/src/gonum.org/v1/plot/text/latex.go:156:3: cannot use
> hdlr.Fonts.Lookup(bf, fnt.Size).Face (type *sfnt.Font) as type
> *truetype.Font in field value
> /home/fred/GO/src/gonum.org/v1/plot/text/latex.go:157:3: cannot use
> hdlr.Fonts.Lookup(bfit, fnt.Size).Face (type *sfnt.Font) as type
> *truetype.Font in field value
> /home/fred/GO/src/gonum.org/v1/plot/text/latex.go:221:3: cannot use
> op.Glyph.Font (type *truetype.Font) as type *sfnt.Font in field value

this should indeed be fixed w/ gonum/plot@v0.9.0.

>
> an update does not fix the error.
>
> go get -u gonum.org/v1/plot/...
> # cd /home/fred/GO/src/github.com/go-latex/latex; git pull --ff-only
> Votre information de configuration indique de fusionner avec la
> référence
> 'refs/heads/master'
> du serveur distant, mais cette référence n'a pas été récupérée.
> package github.com/go-latex/latex/drawtex: exit status 1

this error may come from the fact that go-latex/latex is now using
'main' as its main branch in lieu of 'master'.

hth,
-s

-- 
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/CBFJN9SW6KFZ.1AH0ARTYSCO4O%40zoidberg.


[go-nuts] Re: gonum plot : font error

2021-05-17 Thread 'Carla Pfaff' via golang-nuts
You should really use modules. The module name is "gonum.org/v1/plot", not 
"github.com/gonum/plot".

$ mkdir scatter-demo
$ cd scatter-demo
$ go mod init scatter-demo
$ edit main.go
# Copy and paste code from 
https://gist.github.com/sbinet/602522b7399ead414e279e2261d81095 into main.go
# Replace all instances of "github.com/gonum/plot" by "gonum.org/v1/plot" 
in the imports of main.go:

"gonum.org/v1/plot"

"gonum.org/v1/plot/plotter"

"gonum.org/v1/plot/vg"
$ go mod tidy
$ go build

On Monday, 17 May 2021 at 14:11:01 UTC+2 Fred wrote:

> Hi,
> I have 2 computers with Linux Manjaro and  go version 1.16.3.
> The same scatter plot 
>  works 
> on one computer and produces on the other these already known errors : 
> 
>
> # gonum.org/v1/plot/text
> /home/fred/GO/src/gonum.org/v1/plot/text/latex.go:153:3: cannot use 
> hdlr.Fonts.Lookup(rm, fnt.Size).Face (type *sfnt.Font) as type 
> *truetype.Font in field value
> /home/fred/GO/src/gonum.org/v1/plot/text/latex.go:154:3: cannot use 
> hdlr.Fonts.Lookup(rm, fnt.Size).Face (type *sfnt.Font) as type 
> *truetype.Font in field value
> /home/fred/GO/src/gonum.org/v1/plot/text/latex.go:155:3: cannot use 
> hdlr.Fonts.Lookup(it, fnt.Size).Face (type *sfnt.Font) as type 
> *truetype.Font in field value
> /home/fred/GO/src/gonum.org/v1/plot/text/latex.go:156:3: cannot use 
> hdlr.Fonts.Lookup(bf, fnt.Size).Face (type *sfnt.Font) as type 
> *truetype.Font in field value
> /home/fred/GO/src/gonum.org/v1/plot/text/latex.go:157:3: cannot use 
> hdlr.Fonts.Lookup(bfit, fnt.Size).Face (type *sfnt.Font) as type 
> *truetype.Font in field value
> /home/fred/GO/src/gonum.org/v1/plot/text/latex.go:221:3: cannot use 
> op.Glyph.Font (type *truetype.Font) as type *sfnt.Font in field value
>
> an update does not fix the error.
>
> go get -u gonum.org/v1/plot/...
> # cd /home/fred/GO/src/github.com/go-latex/latex; git pull --ff-only
> Votre information de configuration indique de fusionner avec la référence 
> 'refs/heads/master'
> du serveur distant, mais cette référence n'a pas été récupérée.
> package github.com/go-latex/latex/drawtex: exit status 1
>
> the go/src/gonum.org/v1/plot/font directory is the same on both 
> computers. Do I need to install some missing fonts ?
>
> Thanks in advance,
>
> Fred
>
>

-- 
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/0bff84b8-0756-42df-8604-3035a861f9e7n%40googlegroups.com.


[go-nuts] gonum plot : font error

2021-05-17 Thread Fred
Hi,
I have 2 computers with Linux Manjaro and  go version 1.16.3.
The same scatter plot 
 works on 
one computer and produces on the other these already known errors : 


# gonum.org/v1/plot/text
/home/fred/GO/src/gonum.org/v1/plot/text/latex.go:153:3: cannot use 
hdlr.Fonts.Lookup(rm, fnt.Size).Face (type *sfnt.Font) as type 
*truetype.Font in field value
/home/fred/GO/src/gonum.org/v1/plot/text/latex.go:154:3: cannot use 
hdlr.Fonts.Lookup(rm, fnt.Size).Face (type *sfnt.Font) as type 
*truetype.Font in field value
/home/fred/GO/src/gonum.org/v1/plot/text/latex.go:155:3: cannot use 
hdlr.Fonts.Lookup(it, fnt.Size).Face (type *sfnt.Font) as type 
*truetype.Font in field value
/home/fred/GO/src/gonum.org/v1/plot/text/latex.go:156:3: cannot use 
hdlr.Fonts.Lookup(bf, fnt.Size).Face (type *sfnt.Font) as type 
*truetype.Font in field value
/home/fred/GO/src/gonum.org/v1/plot/text/latex.go:157:3: cannot use 
hdlr.Fonts.Lookup(bfit, fnt.Size).Face (type *sfnt.Font) as type 
*truetype.Font in field value
/home/fred/GO/src/gonum.org/v1/plot/text/latex.go:221:3: cannot use 
op.Glyph.Font (type *truetype.Font) as type *sfnt.Font in field value

an update does not fix the error.

go get -u gonum.org/v1/plot/...
# cd /home/fred/GO/src/github.com/go-latex/latex; git pull --ff-only
Votre information de configuration indique de fusionner avec la référence 
'refs/heads/master'
du serveur distant, mais cette référence n'a pas été récupérée.
package github.com/go-latex/latex/drawtex: exit status 1

the go/src/gonum.org/v1/plot/font directory is the same on both computers. 
Do I need to install some missing fonts ?

Thanks in advance,

Fred

-- 
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/826ba085-1506-4f07-a44e-c334fd7198bdn%40googlegroups.com.


[go-nuts] Re: Generics and parentheses

2021-05-17 Thread Johann Höchtl
watso...@gmail.com schrieb am Mittwoch, 15. Juli 2020 um 04:44:53 UTC+2:

> Guillamets are worth consideration. They are common on European keyboards 
> and avoid all the syntax ambiguities.
>
>
> They are common in the french-speaking part of europe. Even the OPs 
statement 
> A typical computer keyboard provides four easily accessible pairs of 
single-character symmetrical "brackets":
is wrong for the german keyboard layout. Now guillamets? I like the clarity 
of Golang and would not like to see it morph into APL.

-- 
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/50d4eef7-263f-4144-ab1f-797a40104397n%40googlegroups.com.


[go-nuts] Re: Map to struct and vice versa

2021-05-17 Thread Mock ***

Hi Nick,

I understand , please check if this pkg may help you 
https://github.com/fatih/structs,

Thanks 
On Monday, May 17, 2021 at 2:58:54 PM UTC+5:30 Amnon wrote:

> https://programmersought.com/article/93641771999/
>
> On Monday, 17 May 2021 at 10:26:14 UTC+1 nick@gmail.com wrote:
>
>> Is there an easy way to go from a map to a struct and vice versa? e.g. 
>> going from:
>> m := map[string]interface{}{"a": "x", "b": 5}
>>
>> to an instance of:
>>   type T struct {
>> A string
>> B int
>>   }
>>
>> I can do this going through JSON (marshal the map, unmarshal into struct),
>> but is there a more direct way?
>>
>

-- 
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/6b39efc9-c12f-4474-833a-b4cee80876abn%40googlegroups.com.


[go-nuts] Re: Map to struct and vice versa

2021-05-17 Thread Amnon
https://programmersought.com/article/93641771999/

On Monday, 17 May 2021 at 10:26:14 UTC+1 nick@gmail.com wrote:

> Is there an easy way to go from a map to a struct and vice versa? e.g. 
> going from:
> m := map[string]interface{}{"a": "x", "b": 5}
>
> to an instance of:
>   type T struct {
> A string
> B int
>   }
>
> I can do this going through JSON (marshal the map, unmarshal into struct),
> but is there a more direct way?
>

-- 
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/ac267813-f553-4d65-b4eb-4642c640237an%40googlegroups.com.


[go-nuts] Map to struct and vice versa

2021-05-17 Thread Nick Keets
Is there an easy way to go from a map to a struct and vice versa? e.g.
going from:
m := map[string]interface{}{"a": "x", "b": 5}

to an instance of:
  type T struct {
A string
B int
  }

I can do this going through JSON (marshal the map, unmarshal into struct),
but is there a more direct way?

-- 
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/CAPKajN66dC1AzJ542v_W3St%2BytX186XAEJMybp3y0tBb%2BV-1Wg%40mail.gmail.com.