Re: [go-nuts] Representing Optional/Variant Types Idiomatically (or not)

2021-06-19 Thread Henry
Instead of exposing the fields in a struct like that, you may consider 
using constructor function to have better control over what the user can 
set into the struct. That way you don't need to worry about the user 
setting invalid values. For instance,
```Go
type EmailEvent struct{
  id string
  date time.Time 
  description string 
}

func NewEmailEvent(id string, date time.Time, description string) 
EmailEvent {
  return EmailEvent{id,date,description}
}

func NewEmailEventByDescription(description string) EmailEvent {
  return EmailEvent{
id: randomID(), //validate or set the default value for optional or 
missing fields
date: time.Now(),
description: description,
  }
}
```
Or you can getter and setter methods instead of constructors.
```Go
func (e *EmailEvent) SetID(id string) {
   //validate id
   if id == "" {
  //do something
   }
}
```
Or you can set the missing value upon use.
```
func (e EmailEvent) Write(writer io.Writer) {
  if e.id == "" {
e.id = randomID()
  }
  //do processing here
}
```
About type variant, the common way is to use interface as others have 
suggested since it is easily extensible. Alternatively, you may merge them 
into one struct. This is less flexible but may be useful when you are 
dealing with small type variants. For example:
```Go
type EventType int 
const (
   Undefined EventType = iota
   DisplayEvent 
   MailEvent
)

type Event struct {
   from string 
   to string 
   description string
   eventType EventType
}

func NewDisplayEvent(desc string) Event {
  return Event {
 description:desc,
 eventType: DisplayEvent,
  }
}

func NewMailEvent(from, to, desc string) Event {
  return Event {
from: from,
to: to,
description: desc,
eventType: MailEvent,
  }
}

//implement your getters here
```
I hope this helps.
On Friday, June 18, 2021 at 2:45:36 AM UTC+7 ba...@iitbombay.org wrote:

> On Jun 17, 2021, at 1:20 AM, Jan Mercl <0xj...@gmail.com> wrote:
> > 
> > On Thu, Jun 17, 2021 at 9:43 AM Joshua  wrote:
> > 
> >> 1) I'm modelling a data type which has a field which may or may not be
> >> there, would most Gophers reach for a pointer here, and use `nil' to
> >> represent a missing value?
> > 
> > That's the usual approach seen in the wild and IMO often the wrong one.
> > 
> > Unless the size of the field's type is big, I'd suggest just a plain
> > field and a boolean value that represents the "present/valid"
> > information.
>
> This can get error prone as you may forget to update the validity field
> and the error may not be caught easily. And painful if you have more
> than one optional field. Now you need to invent more names or use
> longer names (foo_valid). The pointer alternative will fail more obviously
> if you mess up!
>
> > 
> > Less GC pressure, improved cache locality.
> > 
> > -- 
> > 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.
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/CAA40n-XGP2edWe1b13kpE4PhdFdCsJWGk79z8ngEpj19ycwmxQ%40mail.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/a461c03a-df72-4d67-b0b5-7c4c1ff523ffn%40googlegroups.com.


Re: [go-nuts] Re: Help me troubleshoot this missing value problem

2021-06-19 Thread Tong Sun
Ah, thanks a lot Sean!

On Sat, Jun 19, 2021 at 6:52 PM Sean Liao  wrote:

> You're accessing .SecondKS when it should be .Shortcut.SecondKS (as you
> correctly do for .Shortcut.FirstKS)
>
> https://play.golang.org/p/Jzxws3JaTSQ
>
> side note: I think it's easier to read if you also use indenting in your
> templates and use `{{- -}}` to trim leading/trailing whitespace
>
> On Sunday, June 20, 2021 at 12:05:11 AM UTC+2 sunto...@gmail.com wrote:
>
>> Please take a look at
>>
>> https://play.golang.org/p/k_CM_cJgSvJ
>>
>> line 77 tries to output .FirstKS then .SecondKS if it is not nil
>>
>> However, there are two cases, the debug output shows there is .SecondKS
>> field but the template thinks there is ``:
>>
>> alt MINUS map[Id:EditorCutLineBackward
>> Shortcut:map[FirstKS:alt MINUS SecondKS:control K]] EditorCutLineBackward
>> control C map[Id:RunToCursor Shortcut:map[FirstKS:control C
>> SecondKS:control U]] RunToCursor
>>
>> I've run out of ideas how it could possibly happen.
>>
>> Somebody help please. thx
>>
>>
>> --
> You received this message because you are subscribed to a topic in the
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/golang-nuts/3NUjoj_Bakc/unsubscribe.
> To unsubscribe from this group and all its topics, 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/4c37e558-296f-4274-b7fd-679f4d50954en%40googlegroups.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/CAMmz1OdZfHbHcziN-GjOCbv0uXcoxAADPpEQaOT%3DCUpVpVbDHw%40mail.gmail.com.


[go-nuts] Re: Help me troubleshoot this missing value problem

2021-06-19 Thread Sean Liao
You're accessing .SecondKS when it should be .Shortcut.SecondKS (as you 
correctly do for .Shortcut.FirstKS)

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

side note: I think it's easier to read if you also use indenting in your 
templates and use `{{- -}}` to trim leading/trailing whitespace

On Sunday, June 20, 2021 at 12:05:11 AM UTC+2 sunto...@gmail.com wrote:

> Please take a look at
>
> https://play.golang.org/p/k_CM_cJgSvJ
>
> line 77 tries to output .FirstKS then .SecondKS if it is not nil
>
> However, there are two cases, the debug output shows there is .SecondKS 
> field but the template thinks there is ``:
>
> alt MINUS map[Id:EditorCutLineBackward Shortcut:map[FirstKS:alt 
> MINUS SecondKS:control K]] EditorCutLineBackward
> control C map[Id:RunToCursor Shortcut:map[FirstKS:control C 
> SecondKS:control U]] RunToCursor 
>  
> I've run out of ideas how it could possibly happen. 
>
> Somebody help please. thx
>
>
>

-- 
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/4c37e558-296f-4274-b7fd-679f4d50954en%40googlegroups.com.


[go-nuts] Re: Convert json to css for golang

2021-06-19 Thread Tong Sun
Can you express your converting from json to css using Golang template rule?


On Saturday, June 19, 2021 at 3:51:32 AM UTC-4 muhorto...@gmail.com wrote:

> Hi everyone, I need a converter from json to css.I use 
> https://github.com/ysugimoto, it translates css to json, then I connect 
> some plugins that change the json, and after that I need to recreate it 
> back to css. Do not advise Postcss because I'm just trying to make it 
> similar to Golang
>

-- 
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/b2238961-0316-47f3-be12-bcbcc1d66657n%40googlegroups.com.


[go-nuts] Help me troubleshoot this missing value problem

2021-06-19 Thread Tong Sun
Please take a look at

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

line 77 tries to output .FirstKS then .SecondKS if it is not nil

However, there are two cases, the debug output shows there is .SecondKS 
field but the template thinks there is ``:

alt MINUS map[Id:EditorCutLineBackward Shortcut:map[FirstKS:alt 
MINUS SecondKS:control K]] EditorCutLineBackward
control C map[Id:RunToCursor Shortcut:map[FirstKS:control C 
SecondKS:control U]] RunToCursor 
 
I've run out of ideas how it could possibly happen. 

Somebody help please. thx


-- 
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/c964e9f9-5c68-4c72-bb82-6e97a7b3a443n%40googlegroups.com.


[go-nuts] Re: how to consume API using Golang

2021-06-19 Thread Brian Candler
You don't need to build and encode the Authorization header yourself - you 
can use Request.SetBasicAuth 
.  Building it 
yourself is fine too, if you do it right - google "go http authorization 
basic" for some examples.

Your code has a lot of other problems:
- you shouldn't put semicolons at the ends of lines
- there is no ContentType member in the Request structure - this is a 
header.  It's normally passed as an argument to Post 
.  See the code for client.Post 
 to see how 
to do it by hand
- similarly, the request method is implied by those functions, or passed as 
an argument to NewRequest .
- you seem to be overriding the Host from the url with a different one

On Thursday, 17 June 2021 at 17:19:45 UTC+1 Asim Shahzad wrote:

> Please anyone guide me that how to consume API using Golang with user name 
> and password? 
>
> This is the API address
>
>
> https://emea.universal-api.pp.travelport.com/B2BGateway/connect/uAPI/
> AirService 
>
> if have to pass the following with request
>
> request.Headers.Add("Authorization", "Basic " + 
> Convert.ToBase64String(Encoding.ASCII.GetBytes("username" + ":" + 
> "password"))); 
>   
>
> request.ContentType = "text/xml; encoding='utf-8'";
> 
>
> request.Method = "Post";
>   
>
> request.Headers.Add("SOAPAction", "");
>  
>
> request.Host = "twsprofiler.travelport.com";
>
>
>
> I shall be very thankful to him.
>

-- 
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/9bec743a-2f15-4025-a6f1-ba6f8ee3dffdn%40googlegroups.com.


[go-nuts] Re: unexpected behavior http.MaxBytesReader & streaming response

2021-06-19 Thread Brian Candler
Ah I get it now: it's due to application starting to send a response before 
the request has been completely received, as per the response to your 
ticket which links to https://github.com/golang/go/issues/15527

On Saturday, 19 June 2021 at 11:15:34 UTC+1 Brian Candler wrote:

> Here is a variation on your program, removing json.Encoder from the 
> equation:
> https://play.golang.org/p/ACS_n1yNJfQ
>
> It works as shown, but if you uncomment the commented-out fmt.Fprintf, it 
> fails.  (macOS, go1.16.5)
>
> This is indeed weird. As far as I can see, the only way MaxBytesReader 
>  
> interacts with its Writer (w) argument is to call w.requestTooLarge() if 
> the limit is exceeded *and* w has that method.  But I've checked and it 
> doesn't.
>
> On Saturday, 19 June 2021 at 06:19:22 UTC+1 jerome@gmail.com wrote:
>
>> I submit this issue https://github.com/golang/go/issues/46826
>>
>> Le vendredi 18 juin 2021 à 21:15:54 UTC+2, Jérôme LAFORGE a écrit :
>>
>>> Sorry, I wasn't clear enough. 
>>> I know that panic comes from my code. It throws it. 
>>> But I wanted to show that error on Decode must not occur because the 
>>> body is smaller than 8kB. 
>>> The 2nd example works like this. 
>>> The only difference between the two examples is just the response is 
>>> discarded. 
>>>
>>> Le vendredi 18 juin 2021 à 20:37:11 UTC+2, seank...@gmail.com a écrit :
>>>
 I think it's working as expected?
 The panic originates in your code:

 ```
 err := decoder.Decode()
 if err != nil {
 panic(err)
 }
 ```

 What you should be doing is to break out of the loop and handle the 
 error (client sending more than you want to read) gracefully
 On Friday, June 18, 2021 at 4:32:11 PM UTC+2 jerome@gmail.com 
 wrote:

> Hello,
> I am facing to unexpected behavior when I want to limit the maximum 
> body request and managing the request/response as streaming way.
>
> With this code https://play.golang.org/p/eN2XEpiPdhn, when I send the 
> request, I have this unexpected error (whatever the max size):
>
> ```
> 2021/06/18 15:48:49 http: panic serving 127.0.0.1:39024: http: 
> invalid Read on closed Body
> goroutine 6 [running]:
> net/http.(*conn).serve.func1(0xc000110960)
> net/http/server.go:1824 +0x153
> panic(0x671b60, 0xc12af0)
> runtime/panic.go:971 +0x499
> main.serve(0x713cd0, 0xc0001480e0, 0xc000164000)
> autodebit/service/order/cmd/main.go:32 +0x3b7
> net/http.HandlerFunc.ServeHTTP(0x6d2608, 0x713cd0, 0xc0001480e0, 
> 0xc000164000)
> net/http/server.go:2069 +0x44
> net/http.serverHandler.ServeHTTP(0xc000148000, 0x713cd0, 0xc0001480e0, 
> 0xc000164000)
> net/http/server.go:2887 +0xa3
> net/http.(*conn).serve(0xc000110960, 0x714180, 0xc28280)
> net/http/server.go:1952 +0x8cd
> created by net/http.(*Server).Serve
> net/http/server.go:3013 +0x39b
> ```
>
> But if I discard the response (via io.Discard) that works as expected 
> https://play.golang.org/p/ZOJQz6EC--V
>
> I don't understand what it is going wrong. Can I use 
> http.MaxBytesReader when I send response as streaming way?
>
> Thanks for your help
> Jérôme
>
> the body file seems too big to be in attach, I put it on 
> https://sharetext.me/iveuebzrfq
> the curl cmd: 
> curl POST 'http://127.0.0.1:8081' --data-binary '@body.jsonl'
>
>

-- 
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/300c2261-c478-48b5-ba6c-eeede6a31dfdn%40googlegroups.com.


[go-nuts] Re: unexpected behavior http.MaxBytesReader & streaming response

2021-06-19 Thread Brian Candler
Here is a variation on your program, removing json.Encoder from the 
equation:
https://play.golang.org/p/ACS_n1yNJfQ

It works as shown, but if you uncomment the commented-out fmt.Fprintf, it 
fails.  (macOS, go1.16.5)

This is indeed weird. As far as I can see, the only way MaxBytesReader 
 interacts 
with its Writer (w) argument is to call w.requestTooLarge() if the limit is 
exceeded *and* w has that method.  But I've checked and it doesn't.

On Saturday, 19 June 2021 at 06:19:22 UTC+1 jerome@gmail.com wrote:

> I submit this issue https://github.com/golang/go/issues/46826
>
> Le vendredi 18 juin 2021 à 21:15:54 UTC+2, Jérôme LAFORGE a écrit :
>
>> Sorry, I wasn't clear enough. 
>> I know that panic comes from my code. It throws it. 
>> But I wanted to show that error on Decode must not occur because the body 
>> is smaller than 8kB. 
>> The 2nd example works like this. 
>> The only difference between the two examples is just the response is 
>> discarded. 
>>
>> Le vendredi 18 juin 2021 à 20:37:11 UTC+2, seank...@gmail.com a écrit :
>>
>>> I think it's working as expected?
>>> The panic originates in your code:
>>>
>>> ```
>>> err := decoder.Decode()
>>> if err != nil {
>>> panic(err)
>>> }
>>> ```
>>>
>>> What you should be doing is to break out of the loop and handle the 
>>> error (client sending more than you want to read) gracefully
>>> On Friday, June 18, 2021 at 4:32:11 PM UTC+2 jerome@gmail.com wrote:
>>>
 Hello,
 I am facing to unexpected behavior when I want to limit the maximum 
 body request and managing the request/response as streaming way.

 With this code https://play.golang.org/p/eN2XEpiPdhn, when I send the 
 request, I have this unexpected error (whatever the max size):

 ```
 2021/06/18 15:48:49 http: panic serving 127.0.0.1:39024: http: invalid 
 Read on closed Body
 goroutine 6 [running]:
 net/http.(*conn).serve.func1(0xc000110960)
 net/http/server.go:1824 +0x153
 panic(0x671b60, 0xc12af0)
 runtime/panic.go:971 +0x499
 main.serve(0x713cd0, 0xc0001480e0, 0xc000164000)
 autodebit/service/order/cmd/main.go:32 +0x3b7
 net/http.HandlerFunc.ServeHTTP(0x6d2608, 0x713cd0, 0xc0001480e0, 
 0xc000164000)
 net/http/server.go:2069 +0x44
 net/http.serverHandler.ServeHTTP(0xc000148000, 0x713cd0, 0xc0001480e0, 
 0xc000164000)
 net/http/server.go:2887 +0xa3
 net/http.(*conn).serve(0xc000110960, 0x714180, 0xc28280)
 net/http/server.go:1952 +0x8cd
 created by net/http.(*Server).Serve
 net/http/server.go:3013 +0x39b
 ```

 But if I discard the response (via io.Discard) that works as expected 
 https://play.golang.org/p/ZOJQz6EC--V

 I don't understand what it is going wrong. Can I use 
 http.MaxBytesReader when I send response as streaming way?

 Thanks for your help
 Jérôme

 the body file seems too big to be in attach, I put it on 
 https://sharetext.me/iveuebzrfq
 the curl cmd: 
 curl POST 'http://127.0.0.1:8081' --data-binary '@body.jsonl'



-- 
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/15e79b33-ce8a-49ed-9a06-de92366698ebn%40googlegroups.com.


[go-nuts] Convert json to css for golang

2021-06-19 Thread Денис Мухортов
Hi everyone, I need a converter from json to css.I use 
https://github.com/ysugimoto, it translates css to json, then I connect 
some plugins that change the json, and after that I need to recreate it 
back to css. Do not advise Postcss because I'm just trying to make it 
similar to Golang

-- 
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/ad18bfd9-5b40-4aaa-97a8-d304e3d4cf0fn%40googlegroups.com.