Re: [go-nuts] Re: Query regarding net/http/cookiejar: Cookies() method and Expiry/MaxAge

2022-07-01 Thread Amit Saha
On Fri, Jul 1, 2022 at 7:08 PM Amit Saha  wrote:

>
> On Thu, Jun 30, 2022 at 4:16 PM Volker Dobler 
> wrote:
>
>> On Thursday, 30 June 2022 at 00:31:29 UTC+2 amits wrote:
>>
>>> Currently the Cookies() method as explained at
>>> https://pkg.go.dev/net/http/cookiejar#Jar.Cookies only adds the Name
>>> and Value of the cookies and strips out the MaxAge and Expires field (and
>>> all other fields). Presumably, as I can see in the code, the logic is if a
>>> cookie is returned as a return value from this method, that means, the
>>> cookie is valid  - expiry date is in the future, for example.
>>>
>> Technically neither is "stripped out", the Jar itself knows about these
>> fields, they just aren't returned to the caller of Cookies().
>>
>
> Agree with your interpretation of my observation, rather than mine.
>
>
>>
>>
>>> In the context of testing my HTTP handler, I wanted to make sure that
>>> the expiry of a certain cookie is set to a specific time in the future.
>>>
>> That can be done without putting the cookies into a Jar:
>> Just inspect the raw cookies sent in the Request, e.g. with
>> net/http.Request.Cookies. These cookies have all fields set.
>>
>>
>>> However, the above implementation doesn't make it possible. Is that a
>>> fair expectation to have that the cookiejar's Cookies() method will
>>> preserve the Expires/MaxAge field of the cookie so that I can verify my
>>> HTTP handler function logic?
>>>
>> Are you asking whether it's a fair to expect that
>> net/http/cookiejar.Jar doesn't have bugs? It has a decent
>> set of tests that check expiry of cookies so I think yes,
>> this is a fair expectation.
>>
>>
>>> Or is there another alternative suggestion?
>>>
>> There are open source drop in replacements for
>> net/http/cookiejar.Jar that allow deep inspection of their
>> content, but I really doubt that this is needed.
>>
>> There are two questions:
>> 1) Do your cookies have the right MaxAge? Test that by checking
>> the cookie in the HTTP response.
>>
>
> Yes, that gives me the expected results. I am not sure what I was doing
> wrong and just chased the idea of checking the cookie jar:
>
> // this has the expected cookie max age set
> for _, cookie := range resp.Cookies() {
>  fmt.Printf("Name: %s Value: %s MaxAge: %v Expires: %v\n",
> cookie.Name, cookie.Value, cookie.MaxAge, cookie.Expires)
> }
>
>
>  I am not sure what I was doing wrong

I found out why I wasn't seeing the cookies in my responses but was only
present in the cookie jar:

1. Test client calls handler function 1
2. Handler function 1 sets the cookies and then redirects it to handler
function 2
3. The final response from handler function 2 - doesn't have those cookies
expectedly

So, my solution in this case would be to either:

1. Implement a cookie jar
2. Or check for the cookies by writing a custom redirect function in my
client



>
>
>> 2) Does cookiejar.Jar work properly? You can rely on that;
>> but if you think its tests are lacking: Feel free to provide a CL for
>> the test suite of Jar.
>>
>
> Thanks, my confusions are clear now.
>
>
>>
>> V.
>>
>> --
>> 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/PTmjlCkjmU4/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/ffcf1004-4bcd-44e6-aa08-982e3ecbcaacn%40googlegroups.com
>> <https://groups.google.com/d/msgid/golang-nuts/ffcf1004-4bcd-44e6-aa08-982e3ecbcaacn%40googlegroups.com?utm_medium=email_source=footer>
>> .
>>
>

-- 
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/CANODV3ncsNL2QfWo_vUDDh-1SztUJG8-AKnqsvoP5xvXTSYPLg%40mail.gmail.com.


Re: [go-nuts] Re: Query regarding net/http/cookiejar: Cookies() method and Expiry/MaxAge

2022-07-01 Thread Amit Saha
On Thu, Jun 30, 2022 at 4:16 PM Volker Dobler 
wrote:

> On Thursday, 30 June 2022 at 00:31:29 UTC+2 amits wrote:
>
>> Currently the Cookies() method as explained at
>> https://pkg.go.dev/net/http/cookiejar#Jar.Cookies only adds the Name and
>> Value of the cookies and strips out the MaxAge and Expires field (and all
>> other fields). Presumably, as I can see in the code, the logic is if a
>> cookie is returned as a return value from this method, that means, the
>> cookie is valid  - expiry date is in the future, for example.
>>
> Technically neither is "stripped out", the Jar itself knows about these
> fields, they just aren't returned to the caller of Cookies().
>

Agree with your interpretation of my observation, rather than mine.


>
>
>> In the context of testing my HTTP handler, I wanted to make sure that the
>> expiry of a certain cookie is set to a specific time in the future.
>>
> That can be done without putting the cookies into a Jar:
> Just inspect the raw cookies sent in the Request, e.g. with
> net/http.Request.Cookies. These cookies have all fields set.
>
>
>> However, the above implementation doesn't make it possible. Is that a
>> fair expectation to have that the cookiejar's Cookies() method will
>> preserve the Expires/MaxAge field of the cookie so that I can verify my
>> HTTP handler function logic?
>>
> Are you asking whether it's a fair to expect that
> net/http/cookiejar.Jar doesn't have bugs? It has a decent
> set of tests that check expiry of cookies so I think yes,
> this is a fair expectation.
>
>
>> Or is there another alternative suggestion?
>>
> There are open source drop in replacements for
> net/http/cookiejar.Jar that allow deep inspection of their
> content, but I really doubt that this is needed.
>
> There are two questions:
> 1) Do your cookies have the right MaxAge? Test that by checking
> the cookie in the HTTP response.
>

Yes, that gives me the expected results. I am not sure what I was doing
wrong and just chased the idea of checking the cookie jar:

// this has the expected cookie max age set
for _, cookie := range resp.Cookies() {
 fmt.Printf("Name: %s Value: %s MaxAge: %v Expires: %v\n",
cookie.Name, cookie.Value, cookie.MaxAge, cookie.Expires)
}




> 2) Does cookiejar.Jar work properly? You can rely on that;
> but if you think its tests are lacking: Feel free to provide a CL for
> the test suite of Jar.
>

Thanks, my confusions are clear now.


>
> V.
>
> --
> 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/PTmjlCkjmU4/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/ffcf1004-4bcd-44e6-aa08-982e3ecbcaacn%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/CANODV3%3D9%3Dj3BenbOKX-iwb77q20WAsGeq8dthK%2BfzV8E0EpXwQ%40mail.gmail.com.


Re: [go-nuts] Query regarding net/http/cookiejar: Cookies() method and Expiry/MaxAge

2022-07-01 Thread Amit Saha
On Thu, Jun 30, 2022 at 8:45 AM 'Sean Liao' via golang-nuts <
golang-nuts@googlegroups.com> wrote:

> Implement your own cookie jar,
>
> See also:
> https://github.com/golang/go/issues/19291#issuecomment-282576908
>

Thank you. From the issue comment, I don't quite understand this:

>  If the other fields are returned, than these  cookies would trigger
Set-Cookie headers in package http.

Not a question for you - unless of course you know the answer. But I am
thinking out loud as follows:

CookieJar (client) with cookies with all other fields set -> calls a HTTP
server -> Reads the cookies -> Set-Cookie header flow triggered? (?? -> not
quite sure)




> - sean
>
>
> On Wed, Jun 29, 2022 at 11:31 PM Amit Saha  wrote:
>
>> Hi all,
>>
>> Currently the Cookies() method as explained at
>> https://pkg.go.dev/net/http/cookiejar#Jar.Cookies only adds the Name and
>> Value of the cookies and strips out the MaxAge and Expires field (and all
>> other fields). Presumably, as I can see in the code, the logic is if a
>> cookie is returned as a return value from this method, that means, the
>> cookie is valid  - expiry date is in the future, for example.
>>
>> In the context of testing my HTTP handler, I wanted to make sure that the
>> expiry of a certain cookie is set to a specific time in the future.
>>
>> However, the above implementation doesn't make it possible. Is that a
>> fair expectation to have that the cookiejar's Cookies() method will
>> preserve the Expires/MaxAge field of the cookie so that I can verify my
>> HTTP handler function logic? Or is there another alternative suggestion?
>>
>> Thanks,
>> Amit.
>>
>>
>>
>>
>>
>> --
>> 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/CANODV3%3D6jUAAsKSkJ3iz5t3U6NH2EDSq0jRG02gmPBYd46Afew%40mail.gmail.com
>> <https://groups.google.com/d/msgid/golang-nuts/CANODV3%3D6jUAAsKSkJ3iz5t3U6NH2EDSq0jRG02gmPBYd46Afew%40mail.gmail.com?utm_medium=email_source=footer>
>> .
>>
> --
> 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/PTmjlCkjmU4/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/CAGabyPrVu4%3DC_KLvC_DkQpuw4KSkSSryUagdD1jZ05Ufa8WRoQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/golang-nuts/CAGabyPrVu4%3DC_KLvC_DkQpuw4KSkSSryUagdD1jZ05Ufa8WRoQ%40mail.gmail.com?utm_medium=email_source=footer>
> .
>

-- 
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/CANODV3k1W0xTgqTfeTFVcFhqxn9M5z_Rt7wBqu6FLtvrfwZU%2Bw%40mail.gmail.com.


[go-nuts] Query regarding net/http/cookiejar: Cookies() method and Expiry/MaxAge

2022-06-29 Thread Amit Saha
Hi all,

Currently the Cookies() method as explained at
https://pkg.go.dev/net/http/cookiejar#Jar.Cookies only adds the Name and
Value of the cookies and strips out the MaxAge and Expires field (and all
other fields). Presumably, as I can see in the code, the logic is if a
cookie is returned as a return value from this method, that means, the
cookie is valid  - expiry date is in the future, for example.

In the context of testing my HTTP handler, I wanted to make sure that the
expiry of a certain cookie is set to a specific time in the future.

However, the above implementation doesn't make it possible. Is that a fair
expectation to have that the cookiejar's Cookies() method will preserve the
Expires/MaxAge field of the cookie so that I can verify my HTTP handler
function logic? Or is there another alternative suggestion?

Thanks,
Amit.

-- 
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/CANODV3%3D6jUAAsKSkJ3iz5t3U6NH2EDSq0jRG02gmPBYd46Afew%40mail.gmail.com.


[go-nuts] Re: Setting a cookie - Expires needed with MaxAge?

2022-05-18 Thread Amit Saha
On Thu, May 19, 2022 at 7:59 AM Amit Saha  wrote:
>
>
>
> On Thu, May 19, 2022 at 7:02 AM Amit Saha  wrote:
>>
>> Hi all,
>>
>> For a cookie, it seems like both, `Expires` and `MaxAge` must be
specified? The expectation is that, only Max-Age should be sufficient.
>>
>> // valid
>> c1 := http.Cookie{
>> Expires: time.Now().Add(3600 * 24 * time.Second),
>> MaxAge:  24 * 3600, // 24 hours
>> }
>>
>> // invalid
>> c2 := http.Cookie{
>> MaxAge:  24 * 3600, // 24 hours
>> }
>>
>> Example: https://go.dev/play/p/3PTUM6WKwWS
>>
>> Am I doing something wrong that is not obvious to me?
>
>
> Looking at the code for Valid(), I can see why it gives me that error. So
I assume this is a bug I should report?
>
> Relevant line:
https://github.com/golang/go/blob/c087121dafc1e0250d43b545e85d67e7e6762f74/src/net/http/cookie.go#L242

Filed an issue: https://github.com/golang/go/issues/52989

>>
>>
>> Thanks,
>> Amit.

-- 
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/CANODV3n2GpnuNFrmNY34XNUaSk%2BF2JaxBwq%2BOONO-qOUzONJVQ%40mail.gmail.com.


[go-nuts] Re: Setting a cookie - Expires needed with MaxAge?

2022-05-18 Thread Amit Saha
On Thu, May 19, 2022 at 7:02 AM Amit Saha  wrote:

> Hi all,
>
> For a cookie, it seems like both, `Expires` and `MaxAge` must be
> specified? The expectation is that, only Max-Age should be sufficient.
>
> // valid
> c1 := http.Cookie{
> Expires: time.Now().Add(3600 * 24 * time.Second),
> MaxAge:  24 * 3600, // 24 hours
> }
>
> // invalid
> c2 := http.Cookie{
> MaxAge:  24 * 3600, // 24 hours
> }
>
> Example: https://go.dev/play/p/3PTUM6WKwWS
>
> Am I doing something wrong that is not obvious to me?
>

Looking at the code for Valid(), I can see why it gives me that error. So I
assume this is a bug I should report?

Relevant line:
https://github.com/golang/go/blob/c087121dafc1e0250d43b545e85d67e7e6762f74/src/net/http/cookie.go#L242

>
> Thanks,
> Amit.
>
>

-- 
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/CANODV3k7RSZgjrosjUqRHzofnfjw_kFveR-iaeRyp2X554BZpA%40mail.gmail.com.


Re: [go-nuts] Setting a cookie - Expires needed with MaxAge?

2022-05-18 Thread Amit Saha
On Thu, May 19, 2022 at 7:43 AM Robert Engels  wrote:
>
> Your first case doesn’t even work - nil is output.


That's a nil error - the return value from Valid() - indicating that the
cookie is valid.


>
>
> On May 18, 2022, at 4:11 PM, Amit Saha  wrote:
>
> 
> Hi all,
>
> For a cookie, it seems like both, `Expires` and `MaxAge` must be
specified? The expectation is that, only Max-Age should be sufficient.
>
> // valid
> c1 := http.Cookie{
> Expires: time.Now().Add(3600 * 24 * time.Second),
> MaxAge:  24 * 3600, // 24 hours
> }
>
> // invalid
> c2 := http.Cookie{
> MaxAge:  24 * 3600, // 24 hours
> }
>
> Example: https://go.dev/play/p/3PTUM6WKwWS
>
> Am I doing something wrong that is not obvious to me?
>
> Thanks,
> Amit.
>
> --
> 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/CANODV3nSw7881g9%3DrTbFo9%3DbfyRVtPGEv3toWduUCygQF5B6wg%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/CANODV3mZqKuafWgeyjtVsf%3D7nrUdA4Nu78-RN33z6Z%2B3x3%2BOXw%40mail.gmail.com.


[go-nuts] Setting a cookie - Expires needed with MaxAge?

2022-05-18 Thread Amit Saha
Hi all,

For a cookie, it seems like both, `Expires` and `MaxAge` must be specified?
The expectation is that, only Max-Age should be sufficient.

// valid
c1 := http.Cookie{
Expires: time.Now().Add(3600 * 24 * time.Second),
MaxAge:  24 * 3600, // 24 hours
}

// invalid
c2 := http.Cookie{
MaxAge:  24 * 3600, // 24 hours
}

Example: https://go.dev/play/p/3PTUM6WKwWS

Am I doing something wrong that is not obvious to me?

Thanks,
Amit.

-- 
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/CANODV3nSw7881g9%3DrTbFo9%3DbfyRVtPGEv3toWduUCygQF5B6wg%40mail.gmail.com.


[go-nuts] Re: when EOF returned from http client.Post, client can continue to be used ?

2022-04-18 Thread Amit Saha


On Monday, April 18, 2022 at 6:17:30 PM UTC+10 toon@gmail.com wrote:

> I did experiment but when using the same client I immediately got an EOF 
> again. But that might also have been due to the rate-limiting. 

That's interesting. Could you try printing the response status or writing 
client middleware to further poke into the internals?

>
> I started reading the source code of the http.Client and DefaultTransport 
> but if any could advice good articles/books about the internals to get the 
> most out of it, I would be very interested.
>

You can search online for "Go roundtripper" and use them for some 
experimentation.

My book talks about the transport and connection pooling. Some free preview 
available: 
https://books.google.com.au/books?id=T5FNEAAAQBAJ=PT115=PT137#v=onepage=false


> On Monday, April 18, 2022 at 6:23:22 AM UTC+2 amits...@gmail.com wrote:
>
>> On Sunday, April 17, 2022 at 11:52:17 PM UTC+10 toon@gmail.com wrote:
>>
>>> It is not clear to me from the documentation whether I can continue to 
>>> use an http.Client after a POST failed (due to the connection being closed 
>>> ;  In my case the connection closes probably due to rate-limiting).
>>>
>>> The documentation of the http.Transport does mention that, if a network 
>>> error occurs, it will retry if the request is idempotent. Given I'm doing a 
>>> basicPOST that is not the case for me. 
>>>
>>> But the http.Client will reestablish the connection on the next call to 
>>> Post(...) ?
>>>
>>
>> I believe you will see that's the case. Perhaps worth experimenting with 
>> it? The reason is due to the underlying connection pooling behavior of the  
>> DefaultTransport: https://go.dev/src/net/http/transport.go#L42:
>> " It establishes network connections as needed // and caches them for 
>> reuse by subsequent calls"
>>
>>
>>> toon
>>>
>>>  
>>>
>>

-- 
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/9af66140-e9c0-4eaa-9e5f-99b05f01d42fn%40googlegroups.com.


[go-nuts] Re: when EOF returned from http client.Post, client can continue to be used ?

2022-04-17 Thread Amit Saha


On Sunday, April 17, 2022 at 11:52:17 PM UTC+10 toon@gmail.com wrote:

> It is not clear to me from the documentation whether I can continue to use 
> an http.Client after a POST failed (due to the connection being closed ;  
> In my case the connection closes probably due to rate-limiting).
>
> The documentation of the http.Transport does mention that, if a network 
> error occurs, it will retry if the request is idempotent. Given I'm doing a 
> basicPOST that is not the case for me. 
>
> But the http.Client will reestablish the connection on the next call to 
> Post(...) ?
>

I believe you will see that's the case. Perhaps worth experimenting with 
it? The reason is due to the underlying connection pooling behavior of the  
DefaultTransport: https://go.dev/src/net/http/transport.go#L42:
" It establishes network connections as needed // and caches them for reuse 
by subsequent calls"


> toon
>
>  
>

-- 
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/7e979f11-3f2d-43d5-a5c3-4081a1d06115n%40googlegroups.com.


[go-nuts] Re: sql: converting argument $10 type: unsupported type main.RequestRecord, a struct

2022-01-28 Thread Amit Saha


On Saturday, January 29, 2022 at 5:12:48 AM UTC+11 kimiyas...@gmail.com 
wrote:

> Hi 
>
> i have a struct  event and inner that i have struct RequestRecord
> *type RequestRecord struct {*
> * ID string `json:"id"`*
> * Addr string `json:"addr"`*
> * Host string `json:"host"`*
> * Method string `json:"method"`*
> * UserAgent string `json:"useragent"`*
> *}*
> *type event struct {*
> * ID string `json:"id"`*
> * TimeStamp string `json:"timestamp"`*
> * Action string `json:"action"`*
> * Length int `json:"length"`*
> * Repository string `json:"repository"`*
> * FromRepository string `json:"fromRepository"`*
> * URL string `json:"url"`*
> * Tag string `json:"tag"`*
> * Request RequestRecord `json:"request"`*
> *}*
>
> in inserting to database  i get this error:
> *sql: converting argument $10 type: unsupported type main.RequestRecord, a 
> struct*
>
> i attached main.go
>


Your code uses the `pq` driver but it is using `sqlite` db. Have you looked 
at using a driver for sqlite instead? such as 
https://github.com/mattn/go-sqlite3  

Additionally, i suggest, you share your code in a way, others can run and 
reproduce the error so that they can try to help you and also please share 
the HTTP request you sent to your server which reproduces the error. If i 
try to build your code now, i get this:

# test
./main.go:153:11: too many arguments in call to StoreItem
have (*sql.DB, interface { Scan(interface {}) error; Value() 
(driver.Value, error) }, interface { Scan(interface {}) error; Value() 
(driver.Value, error) })
want (*sql.DB, []event)

-- 
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/e345e3f0-acfa-44a0-a74b-78275553f49en%40googlegroups.com.


[go-nuts] Re: How to distribute database connection across all golang file ?

2022-01-08 Thread Amit Saha
The usual pattern is to have a global *sql.Db object obtained via calling 
the Open() function as explained at [1]. You don't have to worry about 
maintaining the connection pool yourselves as that's automatically done. 
The Go tutorial on the topic [2] shows you can do this using a global 
variable. You can configure various properties of the pool via functions 
such as SetMaxOpenCons [3] amd SetConnMaxIdleTime[4].

If you are using a struct for managing the other configuration in your 
application, you would have the *sql.Db object as a field in that struct 
and then share that struct value with all the parts of your application 
that needs access to it.

[1] https://pkg.go.dev/database/sql#Open
[2] https://go.dev/doc/tutorial/database-access
[3] https://pkg.go.dev/database/sql#DB.SetMaxOpenConns
[4] https://pkg.go.dev/database/sql#DB.SetConnMaxIdleTime

On Sunday, January 9, 2022 at 1:29:07 AM UTC+11 Shishira Pradhan wrote:

> Hello All,
>
> What is the appropriate and industry standard mechanism to send database 
> connection across golang file to use in logic layer to get data from DB? It 
> should be one time connection established and many time use the connection 
> pool across all files.
>
> Thanks,
> Shishira
>

-- 
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/c2c8c729-268f-47c8-b700-9c38ef894f88n%40googlegroups.com.


[go-nuts] Self promotion - New Book, "Practical Go: Building Scalable Network and Non-Network Applications"

2021-12-20 Thread Amit Saha
Hi all, I wanted to share that my latest book is on Go programming and
it is now out electronically as well as in print. It is titled
"Practical Go: Building Scalable Network and Non-Network Applications"
(https://practicalgobook.net/) and published by Wiley publications.

The book targets readers who have learned the language basics -
syntax, understanding of types, being able to write functions (details
are available in the "Getting started" chapter - but essentially, just
a passing familiarity with the language is enough I think). With that
assumption:

In Getting Started, I guide you through the installation of the
software required for the book. I also discuss some of the assumed
knowledge for the following chapters.

In Chapters 1 and 2, I show you how to write command line applications
using the standard library’s flag package.

In Chapters 3 and 4, I show you how you can write robust HTTP clients,
using the standard library’s net/http package and friends.

In Chapters 5, 6 and 7, I show you how you can build production ready
HTTP servers using net/http
and related packages from the standard library.

In Chapters 8, 9 and 10, I show you can build production ready gRPC
applications using the Go gRPC implementation.

In Chapter 11, I show you can work with two categories of data stores
- Object store and a SQL data store using the https://gocloud.dev
library.

In Appendix A, you will learn to implement instrumentation in
applications to emit telemetry data - Logs, Metrics, and Traces.

In Appendix B, you will learn to read configuration data in your
application, learn to build a container image for your application and
get some guidance around deploying your applications.

I wrote this book, not to share with you things I already knew - far
from it. The book is essentially a documentation of my journey of
learning Go and applying it to solve problems which I think based on
my experience are being solved by folks in the industry today.

On that note, you may find bugs/inconsistencies and so if you read the
book, please let me know and/or leave a review.

You can find a detailed table of contents here:
https://practicalgobook.net/toc/ along with a link to an excerpt of
Chapter 1. Any further questions, let me know, i will be happy to
answer!

I wanted to thank all the members of this list who helped me
understand and figure out problems when I found myself stuck when
writing the book.

Thanks,
Amit.

-- 
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/CANODV3nmDyf5CiQWoCrfEp1BFio%2BJHC%3DqjLdZ21842kBpN_opA%40mail.gmail.com.


Re: [go-nuts] Interactive input and "go test"

2021-11-05 Thread Amit Saha
On Fri, Nov 5, 2021 at 11:24 PM Amit Saha  wrote:
>
>
>
> On Fri, 5 Nov 2021, 11:08 pm Axel Wagner,  
> wrote:
>>
>> First, to point out the obvious: It is a bad idea to have a test read from 
>> stdin. You should pass a separate io.Reader and use that.
>>
>> Next: exec.Cmd.{Stdin,Stdout,Stderr} are all set to os.DevNull by default, 
>> meaning you don't get any input or output. I assume
>>
>> - `go test` does not modify Cmd.Stdin (leaving it at os.DevNull) of the 
>> sub-process it launches and redirects Cmd.{Stdout,Stderr} to buffers.
>> - Your test (if you use `exec.Command(…).Run()`) also does not modify any of 
>> them, so they stay at os.DevNull
>> - Other languages have them default to stdin/stdout/stderr of the parent 
>> process, so they try to read from stdin, which is a line-buffered terminal, 
>> so it blocks until you input a line.
>>
>> Again, the solution here should be to not rely on os.Stdin at all, but 
>> instead test a function which uses a general io.Reader.
>
>
> Great thank you for clarifying that.
>
> I ran into this as I was writing a test for an interactive input function and 
> I expected the test to hang but it didn't. So bit of an accidental find and 
> glad I found this.

I posted this as a blog post: https://echorand.me/posts/go-test-stdin/
- in case someone else finds it useful.

Thanks again for your help, Axel.

Best Regards,
Amit.

>
>
>>
>> On Fri, Nov 5, 2021 at 12:48 PM Amit Saha  wrote:
>>>
>>>
>>>
>>> > On 5 Nov 2021, at 10:27 pm, Amit Saha  wrote:
>>> >
>>> > I have this test function:
>>> >
>>> > package main
>>> >
>>> > import (
>>> > "bufio"
>>> > "fmt"
>>> > "os"
>>> > "testing"
>>> > )
>>> >
>>> > func TestInput(t *testing.T) {
>>> > scanner := bufio.NewScanner(os.Stdin)
>>> > msg := "Your name please? Press the Enter key when done"
>>> > fmt.Fprintln(os.Stdout, msg)
>>> >
>>> > scanner.Scan()
>>> > if err := scanner.Err(); err != nil {
>>> > t.Fatal(err)
>>> > }
>>> > name := scanner.Text()
>>> > if len(name) == 0 {
>>> > t.Log("empty input")
>>> > }
>>> > t.Log(name)
>>> >
>>> > }
>>> >
>>> > When i run it via go test -v, this is what i get (TLDR; terminates 
>>> > without waiting for the interactive input):
>>> >
>>> > % go test -v
>>> >
>>> > === RUN   TestInput
>>> >
>>> > Your name please? Press the Enter key when done
>>> >
>>> > stdin_test.go:21: empty input
>>> >
>>> > stdin_test.go:23:
>>> >
>>> > --- PASS: TestInput (0.00s)
>>> >
>>> > PASS
>>> >
>>> > ok  test 0.370s
>>> >
>>> >
>>> >
>>> > However, when i compile the test and then run the binary, it waits for me 
>>> > to enter the input:
>>> >
>>> >
>>> >
>>> > % go test -c
>>> >
>>> > % ./test.test
>>> >
>>> > Your name please? Press the Enter key when done
>>> >
>>> >
>>> >
>>> >
>>> > The latter behavior is more inline with what i was expecting in the first 
>>> > case as well.
>>> >
>>> > I thought may be it has something to do with the fact that go test is 
>>> > executing the binary (i think) after compiling, and started looking at: 
>>> > https://github.com/golang/go/blob/c7f2f51fed15b410dea5f608420858b401887d0a/src/cmd/go/internal/test/test.go
>>> >  , but can't see anything obvious.
>>> >
>>> > Wondering if anyone of you folks have an answer?
>>> >
>>>
>>> Did a bit more of experimentation, and it seems like, that’s just how 
>>> exec.Command(“mycmd").Run() works.
>>>
>>> I tried with exec.Command("wc").Run() and it returns immediately as well. 
>>> (Running the Unix “word count” program).
>>>
>>> I suppose I am surprised since in another language I am familiar with 
>>> (Python), a command waiting for an interactive in

Re: [go-nuts] Interactive input and "go test"

2021-11-05 Thread Amit Saha
On Fri, 5 Nov 2021, 11:08 pm Axel Wagner, 
wrote:

> First, to point out the obvious: It is a bad idea to have a test read from
> stdin. You should pass a separate io.Reader and use that.
>
> Next: exec.Cmd.{Stdin,Stdout,Stderr} are all set to os.DevNull by default,
> meaning you don't get any input or output. I assume
>
> - `go test` does not modify Cmd.Stdin (leaving it at os.DevNull) of the
> sub-process it launches and redirects Cmd.{Stdout,Stderr} to buffers.
> - Your test (if you use `exec.Command(…).Run()`) also does not modify any
> of them, so they stay at os.DevNull
> - Other languages have them default to stdin/stdout/stderr of the parent
> process, so they try to read from stdin, which is a line-buffered terminal,
> so it blocks until you input a line.
>
> Again, the solution here should be to not rely on os.Stdin at all, but
> instead test a function which uses a general io.Reader.
>

Great thank you for clarifying that.

I ran into this as I was writing a test for an interactive input function
and I expected the test to hang but it didn't. So bit of an accidental find
and glad I found this.



> On Fri, Nov 5, 2021 at 12:48 PM Amit Saha  wrote:
>
>>
>>
>> > On 5 Nov 2021, at 10:27 pm, Amit Saha  wrote:
>> >
>> > I have this test function:
>> >
>> > package main
>> >
>> > import (
>> > "bufio"
>> > "fmt"
>> > "os"
>> > "testing"
>> > )
>> >
>> > func TestInput(t *testing.T) {
>> > scanner := bufio.NewScanner(os.Stdin)
>> > msg := "Your name please? Press the Enter key when done"
>> > fmt.Fprintln(os.Stdout, msg)
>> >
>> > scanner.Scan()
>> > if err := scanner.Err(); err != nil {
>> > t.Fatal(err)
>> > }
>> > name := scanner.Text()
>> > if len(name) == 0 {
>> > t.Log("empty input")
>> > }
>> > t.Log(name)
>> >
>> > }
>> >
>> > When i run it via go test -v, this is what i get (TLDR; terminates
>> without waiting for the interactive input):
>> >
>> > % go test -v
>> >
>> > === RUN   TestInput
>> >
>> > Your name please? Press the Enter key when done
>> >
>> > stdin_test.go:21: empty input
>> >
>> > stdin_test.go:23:
>> >
>> > --- PASS: TestInput (0.00s)
>> >
>> > PASS
>> >
>> > ok  test 0.370s
>> >
>> >
>> >
>> > However, when i compile the test and then run the binary, it waits for
>> me to enter the input:
>> >
>> >
>> >
>> > % go test -c
>> >
>> > % ./test.test
>> >
>> > Your name please? Press the Enter key when done
>> >
>> >
>> >
>> >
>> > The latter behavior is more inline with what i was expecting in the
>> first case as well.
>> >
>> > I thought may be it has something to do with the fact that go test is
>> executing the binary (i think) after compiling, and started looking at:
>> https://github.com/golang/go/blob/c7f2f51fed15b410dea5f608420858b401887d0a/src/cmd/go/internal/test/test.go
>> , but can't see anything obvious.
>> >
>> > Wondering if anyone of you folks have an answer?
>> >
>>
>> Did a bit more of experimentation, and it seems like, that’s just how
>> exec.Command(“mycmd").Run() works.
>>
>> I tried with exec.Command("wc").Run() and it returns immediately as well.
>> (Running the Unix “word count” program).
>>
>> I suppose I am surprised since in another language I am familiar with
>> (Python), a command waiting for an interactive input when executed via
>> os/exec would “hang”, for example:
>>
>> % python3
>> Python 3.8.2 (default, Apr  8 2021, 23:19:18)
>> [Clang 12.0.5 (clang-1205.0.22.9)] on darwin
>> Type "help", "copyright", "credits" or "license" for more information.
>> >>> import subprocess
>> >>> subprocess.check_output(["wc”])
>>
>> This will hang
>>
>> >
>> >
>> > Thanks,
>> >
>> > Amit.
>> >
>> >
>> >
>> >
>> >
>> > --
>> > You received this message because you are subscribed to a topic in the
>> Google Groups "golang-nuts"

Re: [go-nuts] Interactive input and "go test"

2021-11-05 Thread Amit Saha



> On 5 Nov 2021, at 10:27 pm, Amit Saha  wrote:
> 
> I have this test function:
> 
> package main
> 
> import (
> "bufio"
> "fmt"
> "os"
> "testing"
> )
> 
> func TestInput(t *testing.T) {
> scanner := bufio.NewScanner(os.Stdin)
> msg := "Your name please? Press the Enter key when done"
> fmt.Fprintln(os.Stdout, msg)
> 
> scanner.Scan()
> if err := scanner.Err(); err != nil {
> t.Fatal(err)
> }
> name := scanner.Text()
> if len(name) == 0 {
> t.Log("empty input")
> }
> t.Log(name)
> 
> }
> 
> When i run it via go test -v, this is what i get (TLDR; terminates without 
> waiting for the interactive input):
> 
> % go test -v
> 
> === RUN   TestInput
> 
> Your name please? Press the Enter key when done
> 
> stdin_test.go:21: empty input
> 
> stdin_test.go:23: 
> 
> --- PASS: TestInput (0.00s)
> 
> PASS
> 
> ok  test 0.370s
> 
> 
> 
> However, when i compile the test and then run the binary, it waits for me to 
> enter the input:
> 
> 
> 
> % go test -c
> 
> % ./test.test 
> 
> Your name please? Press the Enter key when done
> 
> 
> 
> 
> The latter behavior is more inline with what i was expecting in the first 
> case as well. 
> 
> I thought may be it has something to do with the fact that go test is 
> executing the binary (i think) after compiling, and started looking at: 
> https://github.com/golang/go/blob/c7f2f51fed15b410dea5f608420858b401887d0a/src/cmd/go/internal/test/test.go
>  , but can't see anything obvious.
> 
> Wondering if anyone of you folks have an answer?
> 

Did a bit more of experimentation, and it seems like, that’s just how 
exec.Command(“mycmd").Run() works. 

I tried with exec.Command("wc").Run() and it returns immediately as well. 
(Running the Unix “word count” program).

I suppose I am surprised since in another language I am familiar with (Python), 
a command waiting for an interactive input when executed via os/exec would 
“hang”, for example:

% python3
Python 3.8.2 (default, Apr  8 2021, 23:19:18) 
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.check_output(["wc”])

This will hang

> 
> 
> Thanks,
> 
> Amit.
> 
> 
> 
> 
> 
> -- 
> 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/24pL7iQbx64/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/0f48fc8d-7e21-46a6-b736-90ea2f0bcbe6n%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/25A4D3EA-F228-447C-A19E-8BE062840E20%40gmail.com.


[go-nuts] Interactive input and "go test"

2021-11-05 Thread Amit Saha
I have this test function:

package main

import (
"bufio"
"fmt"
"os"
"testing"
)

func TestInput(t *testing.T) {
scanner := bufio.NewScanner(os.Stdin)
msg := "Your name please? Press the Enter key when done"
fmt.Fprintln(os.Stdout, msg)

scanner.Scan()
if err := scanner.Err(); err != nil {
t.Fatal(err)
}
name := scanner.Text()
if len(name) == 0 {
t.Log("empty input")
}
t.Log(name)

}

When i run it via go test -v, this is what i get (TLDR; terminates without 
waiting for the interactive input):

% go test -v

=== RUN   TestInput

Your name please? Press the Enter key when done

stdin_test.go:21: empty input

stdin_test.go:23: 

--- PASS: TestInput (0.00s)

PASS

ok  test 0.370s


However, when i compile the test and then run the binary, it waits for me 
to enter the input:


% go test -c

% ./test.test 

Your name please? Press the Enter key when done


The latter behavior is more inline with what i was expecting in the first 
case as well. 

I thought may be it has something to do with the fact that go test is 
executing the binary (i think) after compiling, and started looking 
at: 
https://github.com/golang/go/blob/c7f2f51fed15b410dea5f608420858b401887d0a/src/cmd/go/internal/test/test.go
 
, but can't see anything obvious.

Wondering if anyone of you folks have an answer?


Thanks,

Amit.



-- 
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/0f48fc8d-7e21-46a6-b736-90ea2f0bcbe6n%40googlegroups.com.


Re: [go-nuts] Does the module name have to include the repo name when you publish a Go module?

2021-09-07 Thread Amit Saha
On Wed, 8 Sept 2021, 5:34 am Dean Schulze,  wrote:

> If you are going to publish a Go module does the module name have to
> include the repo name?  I couldn't find this documented anywhere but
> through trial and error I've discovered that it is required.
>
> I named my module like this and published it to github:
>
> *module key-value-mod*
>
> In another module I declared it to be a dependency
>
> *require github.com/dwschulze/key-value-mod
>  v0.1.0*
>
> When I ran *go get* I got this error:
>
> *$ go get github.com/dwschulze/key-value-mod@v0.1.0
> *
> *go: github.com/dwschulze/key-value-mod@v0.1.0
> : parsing go.mod:*
> *module declares its path as: key-value-mod*
> *but was required as: github.com/dwschulze/key-value-mod
> *
>
> I changed the module's name to include the repo (and retagged that commit
> and pushed both)
>
> *module github.com/dwschulze/key-value-mod
> *
>
> and then it worked.
>
> This is a private repo if that makes any difference.
>
> I also discovered that when you have module problems like this you have to
> clear the mod cache or you end up getting the same error over and over
> again.
>

I came to this conclusion the same way as you did via trial and error. The
go.mod of your module must match its location on the Internet/Network when
you are importing it from another module.



>
> --
> 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/d7c5cc18-6c54-4379-92fe-f313a2c5bc01n%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/CANODV3kq7t21Bw3XSb9TV_4_m%2Bwns4U4WKOrG5UVJgwcbiRJ9g%40mail.gmail.com.


Re: [go-nuts] Re: Logging libraries and standard library compatibility

2021-08-31 Thread Amit Saha
On Tue, Aug 31, 2021 at 9:46 PM Robert Engels  wrote:
>
> The io.Writer is too low level to be useful. Imagine a logger that wants to 
> send an sms on severe errors only.
>
> The best solution is to declare your own logger interface - but similar to 
> the collections discussion it would better if the stdlib declared an ILogger 
> interface (or similar)

I found this FAQ entry on zap's project:

"""
Why aren't Logger and SugaredLogger interfaces?

Unlike the familiar io.Writer and http.Handler, Logger and
SugaredLogger interfaces would include many methods. As Rob Pike
points out, "The bigger the interface, the weaker the abstraction."
Interfaces are also rigid — any change requires releasing a new major
version, since it breaks all third-party implementations.

Making the Logger and SugaredLogger concrete types doesn't sacrifice
much abstraction, and it lets us add methods without introducing
breaking changes.

Your applications should define and depend upon an interface that
includes just the methods you use.
"""

The last sentence is interesting here which is pointing application
authors to, I think define their own interface for logging which can
then be implemented by a concrete type such as standard library's
logger or zap's or zerolog's.



> On Aug 31, 2021, at 3:48 AM, Rupert Paddington  wrote:
>
> If Logger was an interface it would be pretty large and to some extent 
> application-specific, which is undesirable.
>
> Instead inject the sink (io.Writer).
>
> This is preferable in every way:
>
> * the interface is small and universal
> * it eliminates the coupling with any specific library present and future
> * it requires to test the behaviour instead of the collaboration
>
> These are the same principles why for example the http.Handler interface 
> takes an http.ResponseWriter, and then the outcome of the response can be 
> tested by reading from the writer that is passed at testing time.
> On Tuesday, 31 August 2021 at 03:19:29 UTC+1 amits...@gmail.com wrote:
>>
>> On Tue, Aug 31, 2021 at 8:58 AM Kevin Chowski  wrote:
>>
>> 
>>
>>
>> > To the original poster: can you just declare your own Logger-like 
>> > interface and allow users of your library to pass whatever logging 
>> > implementation they have? In what ways are you thinking that you're forced 
>> > to be coupled with the standard library's implementation?
>>
>> Yes, I can do that - that's a good idea actually - although more
>> complicated than one I would have liked for my use-case (more on this
>> in my original post and below), but certainly a feasible one with long
>> term benefits.
>>
>> > In what ways are you thinking that you're forced to be coupled with the 
>> > standard library's implementation?
>>
>> When I first add some logging to my application, I am doing it using
>> the standard library's log.Printf(), log.Println(), log.Fatal() and
>> friends. Now, I reach a point of time where I want to start structured
>> logging but i want to do it incrementally. My existing Printf(),
>> Println() calls should continue to build. The only change I want to do
>> initially is simply change how I am constructing the log.Logger
>> object.
>
> --
> 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/c76931fb-4d7e-40f2-84d3-8923dd280748n%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/874D83D2-6CF1-405A-B500-1399DB77785A%40ix.netcom.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/CANODV3kQ0uy%3Doi2FvJXtvSeY-k9zVU0AX5BB6HpJJCV8D3D5qQ%40mail.gmail.com.


Re: [go-nuts] Re: Logging libraries and standard library compatibility

2021-08-30 Thread Amit Saha
On Tue, Aug 31, 2021 at 8:58 AM Kevin Chowski  wrote:




> To the original poster: can you just declare your own Logger-like interface 
> and allow users of your library to pass whatever logging implementation they 
> have? In what ways are you thinking that you're forced to be coupled with the 
> standard library's implementation?

Yes, I can do that - that's a good idea actually - although more
complicated than one I would have liked for my use-case (more on this
in my original post and below), but certainly a feasible one with long
term benefits.

> In what ways are you thinking that you're forced to be coupled with the 
> standard library's implementation?

When I first add some logging to my application, I am doing it using
the standard library's log.Printf(), log.Println(), log.Fatal() and
friends. Now, I reach a point of time where I want to start structured
logging but i want to do it incrementally. My existing Printf(),
Println() calls should continue to build. The only change I want to do
initially is simply change how I am constructing the log.Logger
object.

-- 
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/CANODV3mkFd6MXFkHcVdBCU%2BLwOVPNGUPeP1TSLKQ2B3rQ_oyaQ%40mail.gmail.com.


Re: [go-nuts] Array of structure and html templates.

2021-08-29 Thread Amit Saha
On Mon, Aug 30, 2021 at 12:01 PM Victor Medrano  wrote:
>
> I.m new to go, and working with a web  aplicación I need to pass a tray of 
> struct to the template.
> something like this
>
> type Month struct {
> Mname string
> Color string
> Id int
> }
>
>
> type Year struct {
> Yname, Color string
> Selected bool
> }
>
> type Data struct {
> Years []*Year
> Months []*Month
> }
> but I haven found the way to fill data Data structure from mysql.
> I came to this group because I found it on a book.

Just to clarify, you want to first create an object of Data struct
type by querying data from a MySQL database and then passing the
object to a template where you will render a HTML page using the data?

If so, for the first step, you will use the database/sql package
(along with the mysql driver) to first create the struct, then execute
the template by passing the object to it. For templates, you may find
https://astaxie.gitbooks.io/build-web-application-with-golang/content/en/07.4.html
and https://echorand.me/posts/golang-templates/ useful.

>
> so thanks.
>
> Victor
>
> --
> 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/ce221a92-ffec-48b5-b635-a0f29ffe932an%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/CANODV3kXFcqa94XCeAzDEZ5XMu_y9BZ3J4P9vB99KVTexA8jHQ%40mail.gmail.com.


[go-nuts] Logging libraries and standard library compatibility

2021-08-28 Thread Amit Saha
Hi all, a huge disclaimer - I am not familiar with the design
decisions and internal details of anything that follows. They are
coming from the point of view of a consumer of the language and the
ecosystem and also someone trying to write about these things for an
audience.

Say I have an application - a HTTP server,  where I initialize a
log.Logger as implemented by the standard library and then I inject
that into the handler functions where they call the Printf() or the
other methods to log stuff. Say I have also configured a middleware to
log data for every request, once again using the Printf() and family
functions.

Now, I want to implement some levelled and/or structured logging. So,
here's my expectation - I should just be able to update the initial
code and everything else should not be changed. Of course, in due
course of time, I will update those to take advantage of the actual
functionality offered by those libraries - levelled logging,
structured logging. But, initially, my code should just build and
work.  Many years back, that's how I really found
https://github.com/sirupsen/logrus useful and exciting. However, some
more performant and recent packages don't seem to have that
compatibility as a goal. For example, https://github.com/uber-go/zap
doesn't AFAICT implement the Printf() or other functions. However,
https://github.com/rs/zerolog does, which is great.

I imagine this possibly is due to the standard library not exporting
publicly available types for - logging handlers, formatters (encoders)
and such that third party packages have difficulty implementing these
in standard library compatible way?

To summarize, I would have really wanted a way to avoid my code
becoming tightly coupled with the logging library I am using but that
seems to be hard at this stage.

What does the community think?

-- 
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/CANODV3%3D_ScP%2BRmhJhjmKEQYMrFmirES_agSrk0LYdqpNSZscGQ%40mail.gmail.com.


Re: [go-nuts] Re: Problem with 0MQ and goLang on Windows

2021-08-23 Thread Amit Saha
On Tue, Aug 24, 2021 at 8:55 AM Dominique Kande Vita
 wrote:
>
> Hi All,
>
> this is a kind of old problem. I am dropping here my solution, just in case 
> someone will search for as crazy all over the net.
> 1. add in your PATH (environement variable) the bin folder where vcpkg has 
> compiled the dll
> 2. install libsodium via vcpkg
> 3. copy the dll : libczmq.dll, libsodium.dll, libzmq-mt-4_3_4.dll in the 
> folder where your golang .exe application is compiled.
>
> and it should work

Thanks.


>
> On Tuesday, December 29, 2015 at 6:09:58 AM UTC+2 chow...@gmail.com wrote:
>>
>> Hi Rene,
>>How did you solve it? I also met the problem "exit status -107374181".
>> Thanks.
>>
>> 在 2014年11月30日星期日 UTC+8下午5:03:55,rene marxis写道:
>>>
>>> Hello Jason
>>>
>>> thanks for your answer.
>>> Of course i tried
>>>   worker, err := zmq.NewSocket(zmq.REQ)
>>> but with no success and no error message...
>>>
>>> The problem seems to be related to the libzmq somehow.
>>>
>>> I tried "go run main.go" from inside a VS-Console and got an error "The 
>>> program could not be started, because libzmq-v120-mt-gd-4_0_4.dll is 
>>> missing".
>>> The application exitied with: "exit status -1073741515"
>>>
>>> After also adding the bin directory from libzmq to the PATH and restarting 
>>> the app, the message did not show up againe and the app exists with:
>>> "exit status -107374181"
>>>
>>> What i don't understand is, why the application is trying to load the 
>>> libzmq-v120-mt-gd-4_0_4.dll. As i wrote in my first question, i had to 
>>> copy/rename that dll in order to be able to do the 'go get 
>>> github.com/pebbe/zmq4'
>>>
>>> Independently from this behavior, for me it seems to be wrong to have to 
>>> rename that dll at all.
>>> Isn't there any possibility to tell 'go get' to use that 
>>> libzmq-v120-mt-gd-4_0_4.dll instead of libzmq.dll ?
>>> I got that "tip" to rename from
>>> https://gist.github.com/michfield/4686876
>>> ...
>>>
>>>
>>>
> --
> 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/373686ad-80ce-4b8b-a500-c5ba6f5fd6f0n%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/CANODV3nKYvdLjrLOc3DsLXzQBLRqdE3YtZY%3DWVMySrL6iZr1LQ%40mail.gmail.com.


Re: [go-nuts] New to Unit Testing - Testing HTTP requests and response codes

2021-08-17 Thread Amit Saha


> On 18 Aug 2021, at 5:47 am, 'Beginner' via golang-nuts 
>  wrote:
> 
> Hey, so I am pretty new to Golang and Unit tests as a whole. Right now I am 
> trying to create some tests for this section of code. I want to test in the 
> case of an unexpected HTTP response but I don't know how or where to begin. I 
> am new to mocking and for my previous tests created mocks to test the good 
> case (with help from my coworker). Can anyone give me advice? What resources 
> to read that tackle HTTP requests unit testing, etc. I really want to 
> understand what's going on and don't want to depend on someone 24/7.
> 
> ```
> case http.StatusCreated:
> //This part is tested 
> default:
> responseBody, err := ioutil.ReadAll(resp.Body)
> if err != nil {
> logger.Errorf("Unexpected HTTP response status %v, 
> unable to read response body", resp.StatusCode)
> } else {
> logger.Errorf("Unexpected HTTP response status %v, 
> response body: %s", resp.StatusCode, string(responseBody))
> }
> 
> return "", ErrUnknownStatusCodeResponse
> }```

Have a look at https://pkg.go.dev/net/http/httptest#ResponseRecorder 
 - I think it should 
guide you in the right direction.

> 
> -- 
> 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/636fa4bd-a3af-40c1-93d5-95255c949fben%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/0063B8F9-4C96-4B5A-BFDD-4BA87E1977A1%40gmail.com.


Re: [go-nuts] Re: What are the options to fake SQL database for testing?

2021-08-06 Thread Amit Saha
On Thu, Jul 29, 2021 at 5:13 PM Brian Candler  wrote:
>
> You might also want to look at podman, which runs containers directly as 
> processes under your own userid with no docker daemon - and buildah, the 
> corresponding tool for creating container images.  I use them quite a lot for 
> building and running container images before deploying them to kubernetes.
>
> I haven't used the go bindings, but apparently they do exist:
> https://podman.io/blogs/2020/08/10/podman-go-bindings.html

Thanks - I will check those out. I am looking for a solution which
will work across operating systems - Linux, Mac OS and Windows. Hence,
I suspect using the docker desktop might be a better solution for me.
I am leaning towards using tests containers go.
>
> According to that, they are running podman as a service with a REST API, 
> which isn't a million miles from a docker daemon - except that the podman 
> service runs as your own userid, not as root.
>
> On Thursday, 29 July 2021 at 06:23:05 UTC+1 amits...@gmail.com wrote:
>>
>> On Thu, Jul 29, 2021 at 4:39 AM Marcin Romaszewicz  wrote:
>> >
>> > I have this exact testing issue at my company, we have many Go services 
>> > which use Postgres in production, but are unit tested against SQLite.
>> >
>> > The latest SQLite covers the vast majority of Postgres queries, so most 
>> > tests simply use an SQLite in-memory DB.
>> >
>> > For the tests which require Postgres- specific functionality, such as 
>> > partitioned tables, for example, we use 
>> > https://github.com/testcontainers/testcontainers-go. This is a library 
>> > which talks to Docker and can create your test prerequisites as docker 
>> > containers and gives you connection information once they're up. This 
>> > makes unit tests incredibly slower, but at least functional.
>> >
>> > The danger with mocking too much is that your unit tests end up testing 
>> > the mocks, and not anything remotely like your runtime environment, so 
>> > we've chosen to mock as little as possible.
>>
>> Thank you for sharing about testcontainers-go. I have come across
>> testcontainers, it sounds like a viable solution as well. As with a
>> lot of things in software, it seems like we just have to see what
>> works for "us".
>>
>> >
>> > -- Marcin
>> >
>> > On Wed, Jul 28, 2021 at 4:09 AM Henry  wrote:
>> >>
>> >> On Wednesday, July 28, 2021 at 3:05:43 PM UTC+7 amits...@gmail.com wrote:
>> >>>
>> >>> That sounds interesting - is the tool generating or is able to
>> >>> generate SQL for different databases? That must have been a pretty big
>> >>> effort to create such an abstraction.
>> >>>
>> >>
>> >> It produces different SQL for different databases. It supports a limited 
>> >> number of databases. Note that quite a number of Go ORM libraries already 
>> >> support multiple databases. So it is not new. The difference is that 
>> >> other ORM libraries usually provide a general purpose data access 
>> >> library, whereas ours generates more specific codes. Other than that, 
>> >> they serve a similar purpose.
>> >>
>> >> --
>> >> 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/9c81746a-4fb4-4fb5-8e5f-605169a3f2afn%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...@googlegroups.com.
>> > To view this discussion on the web visit 
>> > https://groups.google.com/d/msgid/golang-nuts/CA%2Bv29Lv83-4yDijNmukf0Vx%2BoBVZXJPR11bqA_B5CY1mNhOowA%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/4d6f1b9d-ad44-4fc5-b309-beba2a199382n%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/CANODV3%3DxEQOWLWirXLS-75TGU15jWJhgRO_4%3D54%3DL5ZcuvQP4g%40mail.gmail.com.


Re: [go-nuts] Showing effective replace in go modules

2021-08-01 Thread Amit Saha
On Sat, Jul 31, 2021 at 1:32 AM Jay Conrod  wrote:
>
> The go command only applies replace directives from the main module's go.mod 
> file. In this example, replace directives in your module B would be applied, 
> but replace directives in A (or any other dependency) would be ignored. This 
> is important for avoiding conflicts that can't easily be resolved (for 
> example, you depend on X and Y, and they both depend on incompatible forked 
> versions of Z).
>
> The command 'go list -m -json $modulepath' shows information about any module 
> you depend on. In the output, the Replace field will be set if the module is 
> replaced.

Thanks for clearing my confusion as well as the pointer to go list for
showing the data i am looking for.

For someone else looking for the exact command: go list -m -f
'{{.Path}} => {{.Replace}}' is one way to verify this.

>
> On Thu, Jul 29, 2021 at 9:54 PM Amit Saha  wrote:
>>
>> Say, i have a module A (github.com/username/foo/v1)  with a replace of
>> a third-party package in its go.mod:
>>
>> replace github.com/dgrijalva/jwt-go => github.com/golang-jwt/jwt
>> v3.2.1+incompatible
>>
>> Now, i  require module A/v1 package from module A in my module B
>> containing the main package:
>>
>>  require (
>> github.com/username/foo/v1
>>  )
>>
>> I want to make sure that, in my module B, "github.com/golang-jwt/jwt
>> v3.2.1" is pulled in.
>>
>> I could verify that using the https://github.com/Helcaraxan/gomod tool:
>>
>> $ ~/go/bin/gomod reveal | grep jwt
>> 'github.com/dgrijalva/jwt-go' is replaced:
>>github.com/launchdarkly/ld-relay/v6 -> github.com/golang-jwt/jwt @
>> v3.2.1+incompatible
>>
>> Is there a way to verify the above using one of the go mod or go
>> build/list commands?
>>
>> Thanks,
>> Amit.
>>
>> --
>> 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/CANODV3kGLUnA0cz_NTBZnYL5%2BrO7rec4WD6FvBqz9ohfNLMMRw%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/CANODV3%3DH_j-9hGdKGGkVvjxOO4nBz-gPgqL8f__%3D%2BBC36QzvMw%40mail.gmail.com.


[go-nuts] Showing effective replace in go modules

2021-07-29 Thread Amit Saha
Say, i have a module A (github.com/username/foo/v1)  with a replace of
a third-party package in its go.mod:

replace github.com/dgrijalva/jwt-go => github.com/golang-jwt/jwt
v3.2.1+incompatible

Now, i  require module A/v1 package from module A in my module B
containing the main package:

 require (
github.com/username/foo/v1
 )

I want to make sure that, in my module B, "github.com/golang-jwt/jwt
v3.2.1" is pulled in.

I could verify that using the https://github.com/Helcaraxan/gomod tool:

$ ~/go/bin/gomod reveal | grep jwt
'github.com/dgrijalva/jwt-go' is replaced:
   github.com/launchdarkly/ld-relay/v6 -> github.com/golang-jwt/jwt @
v3.2.1+incompatible

Is there a way to verify the above using one of the go mod or go
build/list commands?

Thanks,
Amit.

-- 
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/CANODV3kGLUnA0cz_NTBZnYL5%2BrO7rec4WD6FvBqz9ohfNLMMRw%40mail.gmail.com.


Re: [go-nuts] Re: What are the options to fake SQL database for testing?

2021-07-28 Thread Amit Saha
On Thu, Jul 29, 2021 at 4:39 AM Marcin Romaszewicz  wrote:
>
> I have this exact testing issue at my company, we have many Go services which 
> use Postgres in production, but are unit tested against SQLite.
>
> The latest SQLite covers the vast majority of Postgres queries, so most tests 
> simply use an SQLite in-memory DB.
>
> For the tests which require Postgres- specific functionality, such as 
> partitioned tables, for example, we use 
> https://github.com/testcontainers/testcontainers-go. This is a library which 
> talks to Docker and can create your test prerequisites as docker containers 
> and gives you connection information once they're up. This makes unit tests 
> incredibly slower, but at least functional.
>
> The danger with mocking too much is that your unit tests end up testing the 
> mocks, and not anything remotely like your runtime environment, so we've 
> chosen to mock as little as possible.

Thank you for sharing about testcontainers-go. I have come across
testcontainers, it sounds like a viable solution as well. As with a
lot of things in software, it seems like we just have to see what
works for "us".

>
> -- Marcin
>
> On Wed, Jul 28, 2021 at 4:09 AM Henry  wrote:
>>
>> On Wednesday, July 28, 2021 at 3:05:43 PM UTC+7 amits...@gmail.com wrote:
>>>
>>> That sounds interesting - is the tool generating or is able to
>>> generate SQL for different databases? That must have been a pretty big
>>> effort to create such an abstraction.
>>>
>>
>> It produces different SQL for different databases. It supports a limited 
>> number of databases. Note that quite a number of Go ORM libraries already 
>> support multiple databases. So it is not new. The difference is that other 
>> ORM libraries usually provide a general purpose data access library, whereas 
>> ours generates more specific codes. Other than that, they serve a similar 
>> purpose.
>>
>> --
>> 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/9c81746a-4fb4-4fb5-8e5f-605169a3f2afn%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/CA%2Bv29Lv83-4yDijNmukf0Vx%2BoBVZXJPR11bqA_B5CY1mNhOowA%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/CANODV3m1y82FoD_ic8brVvuwYSo4g4Wdg6pJwyfjypdrxtG%3DbQ%40mail.gmail.com.


Re: [go-nuts] Re: What are the options to fake SQL database for testing?

2021-07-28 Thread Amit Saha
On Wed, Jul 28, 2021 at 12:43 AM Henry  wrote:
>
> Are we talking about testing the domain logic or the data persistence? .
>
> If it is about testing data persistence, rephrasing my earlier response, we 
> do not test data persistence, not very thoroughly anyway, because it is 
> auto-generated by a tool. We did however thoroughly tested the tool (and the 
> code it generates) against actual common SQL databases. It is an upfront 
> investment, so that we do not have to do similar tests for the subsequent 
> projects.
>
> The tool is not open-source, but there are plenty of ORM and ORM-like open 
> source Go libraries around. You may consider writing your own. I think in the 
> long run, it is a worthy investment. When I first started it, I just wanted 
> to save time from having to write repetitive mapper for each data type. Our 
> data access code were quite fragile to changes. The manually typed SQL 
> commands were also error prone, and not everybody in the team is familiar 
> with various SQL dialects. So I wrote the tool to auto generate the necessary 
> code. The tool was then used by my colleagues, and over time they added their 
> own enhancement. Now every time there is a change, I just update the data 
> models, run the tool, and done.
>
> We do a test run against a copy of the actual database before deployment, and 
> the final test run against the actual database during deployment.

Thanks for sharing your experiences and approach to testing. I once
again read this is as taking a layered approach to testing.

I am curious about this point:

>We did however thoroughly tested the tool (and the code it generates) against 
>actual common SQL databases.

That sounds interesting - is the tool generating or is able to
generate SQL for different databases? That must have been a pretty big
effort to create such an abstraction.

>
> On Tuesday, July 27, 2021 at 4:55:46 PM UTC+7 mlevi...@gmail.com wrote:
>>
>> I'm not quite sure the intent is the same? Generating models only reduces 
>> the cognitive and time overhead of going back and forth from the code to the 
>> db. It does not test anything. Having a model that you are sure is a 
>> reflection of the db does not help you know that the logic going with it 
>> works? Maybe there is something I don't understand.
>>
>> BTW I'm interested to know if you tool is open-source? Thx!
>>
>>
>> Le mar. 27 juil. 2021 à 11:49, Henry  a écrit :
>>>
>>> We go-generate the mapping between the data models and the database. It was 
>>> tedious to write such tool, but it was a time well spent. Now we just work 
>>> with data models and not worry about testing the SQL instructions. 
>>> Occasionally, we use SQLite in-memory database to double-check certain 
>>> functionality work as intended.
>>>
>>> We also add code that allow the application to run tests against the actual 
>>> database during deployment.
>>>
>>> On Tuesday, July 27, 2021 at 3:21:23 PM UTC+7 mlevi...@gmail.com wrote:
>>>>
>>>> Hi,
>>>>
>>>> IMO, specific mocks like DATA-DOG's tend to be complicated to use and have 
>>>> behaviors that should not appear in a mock, which would ideally tend to 
>>>> have no logic at all.
>>>> There are several mock packages that you can find here [and BTW if you 
>>>> have time to give feedback I'm currently working on my own, anyway that is 
>>>> completely unrelated].
>>>>
>>>> It really depends on what you are looking for in terms of features. Do you 
>>>> need something that is able to error on syntax problems? That keeps track 
>>>> of the execution flow (i.e. if you insert data in your fake database, are 
>>>> you expecting it to be able to return it to you? Under what conditions?)?
>>>>
>>>> Mocking complex systems like SQL databases is quite hard... What I have 
>>>> seen many times is just to have a "seed-based" local database that is used 
>>>> in local integration tests.
>>>> Another, quite efficient solution is to have a test suite's database 
>>>> initialized once and used for all subsequent unit tests. Once you have 
>>>> good unit tests for your database-interacting low-level functions, and 
>>>> integration tests for the whole flow, you can just mock the db behavior in 
>>>> other higher level unit tests because you know it'll work in production.
>>>>
>>>> Hope this helps!
>>>>
>>>> Le mar. 27 juil. 2021 à 10:04, Amit Saha  a écrit :
>>>>>
>>

Re: [go-nuts] Re: What are the options to fake SQL database for testing?

2021-07-28 Thread Amit Saha
On Tue, Jul 27, 2021 at 7:19 PM Markus Zimmermann  wrote:
>
> We switched from SQLite to PostgreSQL as our "in memory" database. We use an 
> SQL database so we do not have to mock DB calls and maintain an interface. If 
> that is something that is interesting i could trigger a blog post about what 
> we did and why?

That will be great to hear. I am assuming your tests do not use any
PostgreSQL specific functionality? This thread looks interesting as
well [1]

[1] https://news.ycombinator.com/item?id=10002142

>
> On Tuesday, July 27, 2021 at 10:21:23 AM UTC+2 mlevi...@gmail.com wrote:
>>
>> Hi,
>>
>> IMO, specific mocks like DATA-DOG's tend to be complicated to use and have 
>> behaviors that should not appear in a mock, which would ideally tend to have 
>> no logic at all.
>> There are several mock packages that you can find here [and BTW if you have 
>> time to give feedback I'm currently working on my own, anyway that is 
>> completely unrelated].
>>
>> It really depends on what you are looking for in terms of features. Do you 
>> need something that is able to error on syntax problems? That keeps track of 
>> the execution flow (i.e. if you insert data in your fake database, are you 
>> expecting it to be able to return it to you? Under what conditions?)?
>>
>> Mocking complex systems like SQL databases is quite hard... What I have seen 
>> many times is just to have a "seed-based" local database that is used in 
>> local integration tests.
>> Another, quite efficient solution is to have a test suite's database 
>> initialized once and used for all subsequent unit tests. Once you have good 
>> unit tests for your database-interacting low-level functions, and 
>> integration tests for the whole flow, you can just mock the db behavior in 
>> other higher level unit tests because you know it'll work in production.
>>
>> Hope this helps!
>>
>> Le mar. 27 juil. 2021 à 10:04, Amit Saha  a écrit :
>>>
>>> Hi all,
>>>
>>> I am just looking at options to write tests for parts of my application 
>>> that interacts with a SQL database. So far it seems like the community has 
>>> few distinct schools of thought:
>>>
>>> - Mocks (For e.g. using https://pkg.go.dev/github.com/DATA-DOG/go-sqlmock)
>>> - In-memory "real" DB solutions - such as tidb-lite for MySQL 
>>> (https://github.com/WangXiangUSTC/tidb-lite)
>>> - Container based functional/integration style testing
>>>
>>> It will be great to hear if anybody has some other experiences to share.
>>>
>>> Thanks,
>>> Amit.
>>>
>>>
>>> On Saturday, October 10, 2015 at 5:30:20 AM UTC+11 kyle.a...@gmail.com 
>>> wrote:
>>>>
>>>>
>>>> On Thursday, October 8, 2015 at 3:54:08 PM UTC-4, vkoch...@gmail.com wrote:
>>>>>
>>>>> P.S. I don’t see  embedded versions of PostgreSQL or MySql. The SQLite is 
>>>>> not quite reach SQL implementation from enterprise point of view.
>>>>
>>>>
>>>> Maybe have a look at tidb? 
>>>> (https://github.com/pingcap/tidb/blob/master/docs/USAGE.md)
>>>
>>> --
>>> 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/4fbc3ae9-b0ff-4750-bfba-1d58a1dba986n%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/5829cbca-703d-42c4-9338-5f739375568an%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/CANODV3kBK5%2B25Nhf71xFAm0K1WExrZfX3wL6oiPCaza7CT6JsA%40mail.gmail.com.


Re: [go-nuts] Re: What are the options to fake SQL database for testing?

2021-07-28 Thread Amit Saha
On Tue, Jul 27, 2021 at 6:20 PM Levieux Michel  wrote:
>
> Hi,
>
> IMO, specific mocks like DATA-DOG's tend to be complicated to use and have 
> behaviors that should not appear in a mock, which would ideally tend to have 
> no logic at all.
> There are several mock packages that you can find here [and BTW if you have 
> time to give feedback I'm currently working on my own, anyway that is 
> completely unrelated].
>
> It really depends on what you are looking for in terms of features. Do you 
> need something that is able to error on syntax problems? That keeps track of 
> the execution flow (i.e. if you insert data in your fake database, are you 
> expecting it to be able to return it to you? Under what conditions?)?
>
> Mocking complex systems like SQL databases is quite hard... What I have seen 
> many times is just to have a "seed-based" local database that is used in 
> local integration tests.
> Another, quite efficient solution is to have a test suite's database 
> initialized once and used for all subsequent unit tests. Once you have good 
> unit tests for your database-interacting low-level functions, and integration 
> tests for the whole flow, you can just mock the db behavior in other higher 
> level unit tests because you know it'll work in production.
>
> Hope this helps!

Thank you for sharing your thoughts - super helpful. I especially like
your summarized version of what i think of as testing it at various
layers - if you test the code which is close to the DB well, you can
then take advantage of simple mocks at higher levels.


>
> Le mar. 27 juil. 2021 à 10:04, Amit Saha  a écrit :
>>
>> Hi all,
>>
>> I am just looking at options to write tests for parts of my application that 
>> interacts with a SQL database. So far it seems like the community has few 
>> distinct schools of thought:
>>
>> - Mocks (For e.g. using https://pkg.go.dev/github.com/DATA-DOG/go-sqlmock)
>> - In-memory "real" DB solutions - such as tidb-lite for MySQL 
>> (https://github.com/WangXiangUSTC/tidb-lite)
>> - Container based functional/integration style testing
>>
>> It will be great to hear if anybody has some other experiences to share.
>>
>> Thanks,
>> Amit.
>>
>>
>> On Saturday, October 10, 2015 at 5:30:20 AM UTC+11 kyle.a...@gmail.com wrote:
>>>
>>>
>>> On Thursday, October 8, 2015 at 3:54:08 PM UTC-4, vkoch...@gmail.com wrote:
>>>>
>>>> P.S. I don’t see  embedded versions of PostgreSQL or MySql. The SQLite is 
>>>> not quite reach SQL implementation from enterprise point of view.
>>>
>>>
>>> Maybe have a look at tidb? 
>>> (https://github.com/pingcap/tidb/blob/master/docs/USAGE.md)
>>
>> --
>> 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/4fbc3ae9-b0ff-4750-bfba-1d58a1dba986n%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/CANODV3k8sYQkfg7wKLqZidDQi9FfzfHuXdUV4ZuSEKZFL%2B%2BxUQ%40mail.gmail.com.


[go-nuts] Re: What are the options to fake SQL database for testing?

2021-07-27 Thread Amit Saha
Hi all,

I am just looking at options to write tests for parts of my application 
that interacts with a SQL database. So far it seems like the community has 
few distinct schools of thought:

- Mocks (For e.g. using https://pkg.go.dev/github.com/DATA-DOG/go-sqlmock)
- In-memory "real" DB solutions - such as tidb-lite for MySQL 
(https://github.com/WangXiangUSTC/tidb-lite)
- Container based functional/integration style testing

It will be great to hear if anybody has some other experiences to share.

Thanks,
Amit.


On Saturday, October 10, 2015 at 5:30:20 AM UTC+11 kyle.a...@gmail.com 
wrote:

>
> On Thursday, October 8, 2015 at 3:54:08 PM UTC-4, vkoch...@gmail.com 
> wrote:
>
>> P.S. I don’t see  embedded versions of PostgreSQL or MySql. The SQLite is 
>> not quite reach SQL implementation from enterprise point of view.
>>
>
> Maybe have a look at tidb ? (
> https://github.com/pingcap/tidb/blob/master/docs/USAGE.md) 
>

-- 
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/4fbc3ae9-b0ff-4750-bfba-1d58a1dba986n%40googlegroups.com.


[go-nuts] Panic recovery in net/http Server

2021-07-16 Thread Amit Saha
I recently posted a query [1] to the grpc mailing list to clarify a
behavior in the Go gRPC server - a panic() in a request handler
function would terminate the server process. Now, I know that's not
the case in the net/http server implementation - a demo here [2].

However, the interesting thing that I read in one of the replies is
that this isn't considered something that is desirable in the net/http
package either. but is a historical choice that we wish could have
been different. I was wondering if someone here would know about that
and why it may not be desirable to have automatic recovery in the http
server implementation because even with the automatic recovery in
place:

1. The application author/operator user will get to know when a panic
has occurred unless the
ErrAbortHandler value is used to panic.
2. The application author can still implement a middleware and not let
the stdlib's panic handling mechanism get
invoked at all to run any custom cleanup for example.


[1] https://groups.google.com/g/grpc-io/c/J8T43gWMwnI
[2] https://gist.github.com/amitsaha/ec7ce96e47bbfca0e7a52d5bff53485b

-- 
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/CANODV3nGDhPGzAPNyr6KZAuD6uU6jr%3DOYasCjZ%3DJeJbYRiir3A%40mail.gmail.com.


Re: [go-nuts] Re: Go on Windows 10 ARM

2021-07-14 Thread Amit Saha
On Thu, 15 July 2021, 1:32 am Nate,  wrote:

> Go 1.17rc1 is available, and you may now grab a copy for Windows 10 ARMv8
> on the downloads page .


Thank you.


> On Wednesday, June 30, 2021 at 10:14:14 PM UTC-4 amits...@gmail.com wrote:
>
>> Hi all - I am curious if anybody has installed the Go toolchain on
>> Windows 10 ARM 64?
>>
>> Thanks,
>> Amit.
>>
> --
> 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/ff9f222a-d3c7-4e3d-b827-aa2c3cb928b3n%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/CANODV3kMRXKExRqt3D2jurY%2B1heGOaCyJ6G7mX74-ySzX72mOA%40mail.gmail.com.


Re: [go-nuts] Go on Windows 10 ARM

2021-07-01 Thread Amit Saha
On Fri, 2 July 2021, 12:36 am Federico Damián Schonborn, <
fdschonb...@gmail.com> wrote:

> Support for Windows ARM64 is being added in Go 1.17 (currently in beta),
> see: https://tip.golang.org/doc/go1.17#windows
>

Thanks.


> El jue, 1 de jul. de 2021 a la(s) 02:15, Amit Saha (amitsaha...@gmail.com)
> escribió:
>
>> On Thu, Jul 1, 2021 at 12:52 PM Kurtis Rader 
>> wrote:
>> >
>> > On Wed, Jun 30, 2021 at 7:14 PM Amit Saha 
>> wrote:
>> >>
>> >> Hi all - I am curious if anybody has installed the Go toolchain on
>> >> Windows 10 ARM 64?
>> >
>> >
>> > You might need to clarify your question. Go is officially supported
>> onWindows 10 ARM64 and you can see the current build status at
>> https://build.golang.org/. Are you experiencing problems with Go on that
>> platform?
>>
>> Sure.  When I go to https://golang.org/dl/ i don't see a installer or
>> a build .tar.gz for Windows ARM 64, so i was wondering if there are
>> folks here building/using Go toolchain on Windows 10 ARM64, how are
>> they doing it.
>>
>>
>> >
>> > --
>> > 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/CANODV3%3DxoviC0Q%2BSsfY-mJ0bRsU5ZkSEc8feAc%3DTjxpRQk_37g%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/CANODV3kCiYdEZe-tJJg5Bh6Lzx7QGb7n-gWszeK-ZHW2n5%2Barg%40mail.gmail.com.


Re: [go-nuts] Go on Windows 10 ARM

2021-06-30 Thread Amit Saha
On Thu, Jul 1, 2021 at 12:52 PM Kurtis Rader  wrote:
>
> On Wed, Jun 30, 2021 at 7:14 PM Amit Saha  wrote:
>>
>> Hi all - I am curious if anybody has installed the Go toolchain on
>> Windows 10 ARM 64?
>
>
> You might need to clarify your question. Go is officially supported onWindows 
> 10 ARM64 and you can see the current build status at 
> https://build.golang.org/. Are you experiencing problems with Go on that 
> platform?

Sure.  When I go to https://golang.org/dl/ i don't see a installer or
a build .tar.gz for Windows ARM 64, so i was wondering if there are
folks here building/using Go toolchain on Windows 10 ARM64, how are
they doing it.


>
> --
> 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/CANODV3%3DxoviC0Q%2BSsfY-mJ0bRsU5ZkSEc8feAc%3DTjxpRQk_37g%40mail.gmail.com.


[go-nuts] Go on Windows 10 ARM

2021-06-30 Thread Amit Saha
Hi all - I am curious if anybody has installed the Go toolchain on
Windows 10 ARM 64?

Thanks,
Amit.

-- 
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/CANODV3%3DVvKk2n5RUzgZe8BLC6BZMhwXMKmkf8rPjBbm%3DSxuWQQ%40mail.gmail.com.


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

2021-06-17 Thread Amit Saha
On 17 Jun 2021, at 5:43 pm, Joshua  wrote:

Hello,

I have 2 questions about idiomatic ways to represent optional values,
and variant values in types.

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?

2) Another field of this type contains an "Action", the variants of
   which hold different information. In nonsense code, I want this:

   ,
   | type EmailAction struct {
   |  Summary string
   |  Decsription string
   |  To string
   |  Attendee person
   |  }
   |
   | type DisplayAction struct {
   |Description string
   |
   |}
   |
   | type Event struct {
   |OptionalField *MyType
   |Action EmailAction OR DisplayAction
   |}
   `


How would you go about modelling this type in Go?  Or alternatively, do
you think trying to use "Type Driven Design" like this is not a good fit
for Go, and rather I should just have a generic set of components and
validate when functions call it.

Personally I prefer the previous, I really like the idea of knowing a
type can't exist in an invalid state! Helps me not have to keep track of
so much in my head.

I will share my thinking for (2):

You could do something like this:

type Action interface {
# your methods
}

type Event struct {
OptionalField *MyType
Action field
}

In your code:

e := Event{..}
Switch e.field.(type):
  case “EmailAction”: do somehting
  case “DisplayAction”: do something else
  default: Invalid action
}



-- 
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/8ed722d4-9111-4e23-aa97-f6601f964f0cn%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/CANODV3kkcuPcx-TqkM36JD9cJQwT4odqkitcHu%2B5j9zyc4cqVw%40mail.gmail.com.


Re: [go-nuts] Re: Understanding how []byte("astring") works

2021-06-14 Thread Amit Saha
On Mon, Jun 14, 2021 at 11:42 AM peterGo  wrote:
>
> Amit,
>
> Compilers implement a specification:
>
> The Go Programming Language Specification
> https://golang.org/ref/spec
>
> Conversions
>
> A conversion changes the type of an expression to the type specified by the 
> conversion. A conversion may appear literally in the source, or it may be 
> implied by the context in which an expression appears.
>
> Conversions to and from a string type
>
> 4. Converting a value of a string type to a slice of bytes type yields a 
> slice whose successive elements are the bytes of the string.
>
> []byte("hellø")   // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}
> []byte("")// []byte{}
>
> MyBytes("hellø")  // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}

Thank you.

>
> Peter
>
> On Sunday, June 13, 2021 at 8:24:57 PM UTC-4 amits...@gmail.com wrote:
>>
>> Hi - My main motivation to understand this is i always had to google
>> this - how to convert a string to a byte slice.
>>
>> Is []byte a type that has been defined in the compiler?
>>
>> Or, is that an internal level detail that an earlier stage (parsing)
>> takes care of when the compiler sees that statement?
>>
>> Thanks,
>> Amit
>
> --
> 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/34100c93-63d9-43b9-a36c-bf2aaf324c21n%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/CANODV3mQpP%2BWRetpyyVK_Q_VYeCVE_sn-9GqsqdVdyDDhK57uw%40mail.gmail.com.


Re: [go-nuts] Understanding how []byte("astring") works

2021-06-14 Thread Amit Saha
On Mon, Jun 14, 2021 at 3:23 PM Roland Müller  wrote:
>
> Hello,
>
> Am Mo., 14. Juni 2021 um 03:24 Uhr schrieb Amit Saha :
>>
>> Hi - My main motivation to understand this is i always had to google
>> this - how to convert a string to a byte slice.
>>
>> Is []byte a type that has been defined in the compiler?
>>
>> Or, is that an internal level detail that an earlier stage (parsing)
>> takes care of when the compiler sees that statement?
>>
>> Thanks,
>> Amit
>>
>
> a []byte is a sequence of octets and strings in Go consistent of a bytes. 
> These bytes represent an sequence of unicode characters according to UTF-8. 
> One such character consists of either a single or two bytes. ASCII -only 
> strings than have as many bytes as UTF characters.
>
> In the example I made two loop functions loopStringByBytes(s) and 
> loopStringByChars(s) and checked them against a ASCII string and a cyrillic 
> string. You can see that for second string every character occupies 2 bytes.
>
> https://play.golang.org/p/DDSpiFuR8Lp

Thank you.

>
> BR,
> Roland
>
>
>>
>> --
>> 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/CANODV3nBYmshDLFwUUdnnyVuvpjhWnwBJOb%3DwrZKEHXmtBgbSg%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/CANODV3k6XpFuP943bC7CCT9nnnFz5J_-trL%3D7rAgowsgWZj%3Drg%40mail.gmail.com.


Re: [go-nuts] Understanding how []byte("astring") works

2021-06-14 Thread Amit Saha
On Mon, Jun 14, 2021 at 4:44 PM Axel Wagner
 wrote:
>
> Hi,
>
> On Mon, Jun 14, 2021 at 2:24 AM Amit Saha  wrote:
>>
>> Hi - My main motivation to understand this is i always had to google
>> this - how to convert a string to a byte slice.
>>
>> Is []byte a type that has been defined in the compiler?
>
>
> No, but `byte` is. It is a predeclared identifier, referring to a builtin 
> integer type (it's an alias for `uint8`).
> So the compiler knows what a `byte` is and what a slice is, so it knows what 
> a slice of byte is.
>
> The conversion between a slice of `byte`s and a `string` is then defined in 
> the spec:
>
>> A non-constant value x can be converted to type T in any of these cases:
>>
>> […]
>>
>> x is an integer or a slice of bytes or runes and T is a string type.
>>
>> x is a string and T is a slice of bytes or runes.
>
> This means the compiler, when seeing a conversion, explicitly tests for these 
> two cases.
>
> How the conversion then actually works, you can see in the compiler explorer. 
> The compiler emits a call into a function called `runtime.stringtoslicebyte`.
> You can actually find that function in the source code of the runtime then 
> (the same function also contains other functions implementing similar 
> conversions).
> Really, it just allocates a new `[]byte` of the appropriate size and then 
> copies over the bytes from the string.
>
>> Or, is that an internal level detail that an earlier stage (parsing)
>
>
> You can actually mess with this a little bit to show that it's not done in 
> the parsing stage, but that the compiler actually knows if a type is a slice 
> of bytes or not.
> Because `byte` is a predeclared identifier, it can be shadowed, like every 
> other identifier. That is, you can declare your *own* type called `byte`:
> https://play.golang.org/p/6vDjw9eWX9s
> You can also define your own alias for `uint8` and then convert that:
> https://play.golang.org/p/nvseU7ofRru
> Or you can do both - first shadown the builtin `byte` alias and *then* create 
> your own:
> https://play.golang.org/p/1Y80stIxa5v
> You can also define your own type called `uint8` and then define `byte` as an 
> alias to that and see that you can no longer convert:
> https://play.golang.org/p/xUQrAmMm5Km
>
> All of this shows that the compiler really can't just rely on parsing and the 
> name. It really needs to have a notion of whether something is a slice of a 
> specific pre-declared type `uint8`, no matter what it is called in the source 
> code.
>
> It does that by creating a "virtual" package builtin inside the compiler and 
> then synthesizing type-definitions in that package. The code for that is here.
> But it should be noted that this package doesn't really exist and behaves 
> significantly different from "normal" packages - not just because it is 
> implemented entirely in the compiler/runtime, but also because it's 
> identifier are defined "outside" any package, in the universe block.
>
> I assume this covers all questions :) Let us know if you have follow-ups :)

Thank you! That's enough for me to start digging in for a bit.
>
> Axel
>
>> takes care of when the compiler sees that statement?
>>
>> Thanks,
>> Amit
>>
>> --
>> 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/CANODV3nBYmshDLFwUUdnnyVuvpjhWnwBJOb%3DwrZKEHXmtBgbSg%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/CANODV3nw-RH1wdj4Zx2%2BY1UMojZSjFrXFBy_MA3rvmC-vW9bTQ%40mail.gmail.com.


[go-nuts] Understanding how []byte("astring") works

2021-06-13 Thread Amit Saha
Hi - My main motivation to understand this is i always had to google
this - how to convert a string to a byte slice.

Is []byte a type that has been defined in the compiler?

Or, is that an internal level detail that an earlier stage (parsing)
takes care of when the compiler sees that statement?

Thanks,
Amit

-- 
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/CANODV3nBYmshDLFwUUdnnyVuvpjhWnwBJOb%3DwrZKEHXmtBgbSg%40mail.gmail.com.


Re: [go-nuts] unpacking map[string]interface{} answer

2021-05-31 Thread Amit Saha


> On 31 May 2021, at 6:08 pm, natxo@gmail.com  
> wrote:
> 
> ok, I think I understand how this library works, and posting this for future 
> reference in case someone needs it.
> 
> The library uses this other library: 
> https://github.com/mitchellh/mapstructure 
> 
> 
> So now I can do this and vim go will helpfully fill in the struct fields for 
> me.
> 
> client := foreman.Client("foreman.l.example.org 
> ", "admin", "whatever", false, "")
> 
> resp, err := client.Get("hosts")
> if err != nil {
> fmt.Println(err)
>  }
> 
> var host foreman.Host
> err = mapstructure.Decode(resp, )
> if err != nil {
> fmt.Println(err)
>  }
>  fmt.Println(host.Capabilities...)
>   Capabilities  m []interface{}
>  }Architecture_id   m int
> ~Build m bool
> ~Comment   m string
> ~Compute_attributesm 
> foreman.Compute_attributes
> ~Compute_profile_idm int
> ~Compute_resource_id   m int
> ~Domain_id m int
> ~Enabled   m bool
> ~Environment_idm int
> ~Host_parameters_attributes_mapm 
> map[string]foreman.Params_archetype
> ~Hostgroup_id  m int
> ~Image_id  m int
> ~Interfaces_attributes_array   m 
> []foreman.Interfaces_attributes
> ~Ipm string
> ~Location_id   m int
> ~Mac   m string
> ~Managed   m bool
> ~Medium_id m int
> ~Model_id  m int
> ~Name  m string
> ~Operatingsystem_idm int
> ~Organization_id   m int
> ~Owner_id  m int
> ~Owner_typem string
> ~Progress_report_idm string
> ~Provision_method  m string
> ~Ptable_id m int
> ~Puppet_ca_proxy_idm int
> ~Puppet_proxy_id   m int
> ~Puppetclass_ids   m []int
> ~Realm_id  m int
> ~Root_pass m string
> ~Subnet_id m int
> ~Compute_attributes.Corespersocket m string
> ~Compute_attributes.Cpus   m string
> ~Compute_attributes.Start  m string
> 
> Thanks for your pointer.

In case you think this is a bit overkill for you and you may choose to do 
something “simpler", you can call the foreman API yourself and then you could 
use something like https://mholt.github.io/json-to-go/ 
 to parse the JSON yourself in your 
program.

> 
> Regards,
> Natxo
> On Monday, May 31, 2021 at 6:45:36 AM UTC+2 natxo@gmail.com 
>  wrote:
> hi,
> 
> I got privately an answer to get the type of variable, so I tried this:
> 
> client := foreman.Client("foreman.l.example.org 
> ", "admin", "whatever", false, "")
>   resp, err := client.Get("hosts")
>   if err != nil {
>   fmt.Println(err)
>   }
>   
>   fmt.Printf("%T\n", resp)
> 
> and the result is: 
> map[string]interface {}
> 
> so this is my problem, I don't know what for type in resp is :?.
> 
> I'll take a look a the external library..
> 
> Thanks!
> On Monday, May 31, 2021 at 12:47:36 AM UTC+2 natxo@gmail.com 
>  wrote:
> hi,
> 
> I am struggling to understand how to use this map[string]interface{}.
> 
> snippet:
> 
> package main
> 
> import "fmt"
> import "github.com/mattwilmott/go-foreman 
> "
> 
> func main() {
> 
> client := foreman.Client("foreman.l.example.org 
> ", "admin", "whatever", false, "")
> fmt.Println(client)
> resp, err := client.Get("hosts")
> if err != nil {
> fmt.Println(err)
> }
> 
> fmt.Println(resp)
> 
> }
> Foreman GET: map[page:1 per_page:20 

Re: [go-nuts] Re: signal.NotifyContext - Does it expose the signal handled?

2021-05-21 Thread Amit Saha
Thanks.


On Sat, 15 May 2021, 12:48 am Vladimir Varankin,  wrote:

> I don't think the current API provides that functionality.
>
> In the original proposal for signal.NotifyContext, there were several
> discussions, including related to what features the new API should provide.
> The consensus was that if a user wants to make a decision base on the
> signal caught, they should use "signal.Notify", as the low-level API, which
> allows that (refer to this issue https://github.com/golang/go/issues/37255
> ).
>
>>
>>
>>
> On Thursday, May 13, 2021 at 10:54:39 PM UTC+2 amits...@gmail.com wrote:
>
>> Hi - i took a brief look and it doesn't look like we can access the
>> signal that was handled via the signal.NotifyContext() function call?
>> It could be useful for logging messages.
>>
> --
> 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/b7b21dd2-b12a-4a01-b2f0-3dee10778cd1n%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/CANODV3kaDAGrP0xqo-y%2BkJ57EOPJmKBrEHtNXMjrQwcZLsLTaQ%40mail.gmail.com.


[go-nuts] signal.NotifyContext - Does it expose the signal handled?

2021-05-13 Thread Amit Saha
Hi - i took a brief look and it doesn't look like we can access the
signal that was handled via the signal.NotifyContext() function call?
It could be useful for logging messages.

-- 
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/CANODV3kuHdtgAJzhKVhHEOd2cK4hg-3Q%3DQwN_OGfcjJ8O0Htxw%40mail.gmail.com.


Re: [go-nuts] Fwd: protoc-gen-go and go modules

2021-05-13 Thread Amit Saha
On Thu, 13 May 2021, 11:20 am Jim Idle,  wrote:

> Create a separate module that contains only the .proto files and use the
> generate directive. You can then version that module and share it. If your
> client is some other language, you can generate Its code from the same repo
> at the same version using your CI. For instance generate a pip wheel as
> well as the go source.
>
> By checking in the generated code for all languages, you guarantee that it
> is in a particular state and that your build is reproducible. It sounds
> counter intuitive, but Makes sense once you've cogitated on it.
>

Thanks. I think I get it. So the repo containing the .proto files would
have a .go file with the generate directives to do the necessary
generation. And the go.mod file of course.


> On Wed, May 12, 2021 at 18:47 Amit Saha  wrote:
>
>> Hi all, this is only relevant to people using grpc.  I posted the query
>> below to the grpc mailing list, thought of posting it here as welll.
>>
>> How are you importing the generated definitions and other resources
>> them into your server and client? As far as I see it now, the protoc
>> command will not generate a go.mod file in the generated code which
>> means - you have to do it manually and then import them into your
>> server and client. That is, you will have three modules:
>>
>> - One for server
>> - One for client
>> - One for the generated code
>>
>> This seems to be the cleanest approach. During local development, we
>> will then use the "replace" directive for iteration.
>>
>> I am keen to hear how it's being done by others in the community.
>>
>> Thanks,
>> Amit.
>>
>> --
>> 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/CANODV3kqN0wtK5bO3AfVbUhMt1iE9eYvboqbzmSLnY72N6-a6A%40mail.gmail.com
>> <https://groups.google.com/d/msgid/golang-nuts/CANODV3kqN0wtK5bO3AfVbUhMt1iE9eYvboqbzmSLnY72N6-a6A%40mail.gmail.com?utm_medium=email_source=footer>
>> .
>>
>

-- 
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/CANODV3myjXgHC9zuxR1J0tPOjDqOXeGVgZ%3DQE9UijVLTPBqFsw%40mail.gmail.com.


[go-nuts] Fwd: protoc-gen-go and go modules

2021-05-12 Thread Amit Saha
Hi all, this is only relevant to people using grpc.  I posted the query
below to the grpc mailing list, thought of posting it here as welll.

How are you importing the generated definitions and other resources
them into your server and client? As far as I see it now, the protoc
command will not generate a go.mod file in the generated code which
means - you have to do it manually and then import them into your
server and client. That is, you will have three modules:

- One for server
- One for client
- One for the generated code

This seems to be the cleanest approach. During local development, we
will then use the "replace" directive for iteration.

I am keen to hear how it's being done by others in the community.

Thanks,
Amit.

-- 
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/CANODV3kqN0wtK5bO3AfVbUhMt1iE9eYvboqbzmSLnY72N6-a6A%40mail.gmail.com.


Re: [go-nuts] New advanced, CGo-free SQLite package

2021-05-01 Thread Amit Saha
On Sun, May 2, 2021 at 12:55 AM Ross Light  wrote:
>
> The original is still there! https://github.com/crawshaw/sqlite
>
> The story for this package is that I used crawshaw.io/sqlite for a number of 
> projects, realized that I wanted some features that were unlikely to be 
> upstream-able -- for example, the switch to modernc.org/sqlite drops Windows 
> support (at least as of 2021-04-30), and I wanted to rename some symbols. 
> Rather than put burden on the maintainers to review a bunch of large PRs and 
> have them weigh the benefits, I decided to create a fork instead.

Ah, thanks! About the fork, I sent a PR for your github repo fixing
the URL, which is what led me to quickly think that the upstream was
missing.

>
> -Ross
>
> On April 30, 2021, Amit Saha  wrote:
>
> Hi Ross, Thanks for sharing this. I am curious what's the difference
> between your fork and the original repo (which is now gone it seems)?
>
> On Fri, Apr 30, 2021 at 11:42 PM Ross Light  wrote:
> >
> > I've created a new Go package for SQLite: zombiezen.com/go/sqlite
> >
> > It is a fork of crawshaw.io/sqlite that uses modernc.org/sqlite, a CGo-free 
> > SQLite package. It aims to be a mostly drop-in replacement for 
> > crawshaw.io/sqlite. It includes APIs for streaming blob I/O, schema 
> > migrations, and user-defined functions.
> >
> > The repository also includes a go fix-like tool to convert code that uses 
> > crawshaw.io/sqlite to use the equivalent zombiezen.com/go/sqlite APIs.
> >
> > Try it out and let me know what you think!
> >
> > Thanks to Jan Mercl and David Crawshaw for early feedback and the amazing 
> > packages this builds upon.
> >
> > -Ross
> >
> > --
> > 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/3878d18f33bea1eb1d1805bd17705e612da337e8%40hey.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/CANODV3nqc78%3DQmSdB4q95a%3DJpfBwvYtaAnsPcSRGAT50T6jHmA%40mail.gmail.com.


Re: [go-nuts] New advanced, CGo-free SQLite package

2021-05-01 Thread Amit Saha
Hi Ross, Thanks for sharing this. I am curious what's the difference
between your fork and the original repo (which is now gone it seems)?

On Fri, Apr 30, 2021 at 11:42 PM Ross Light  wrote:
>
> I've created a new Go package for SQLite: zombiezen.com/go/sqlite
>
> It is a fork of crawshaw.io/sqlite that uses modernc.org/sqlite, a CGo-free 
> SQLite package. It aims to be a mostly drop-in replacement for 
> crawshaw.io/sqlite. It includes APIs for streaming blob I/O, schema 
> migrations, and user-defined functions.
>
> The repository also includes a go fix-like tool to convert code that uses 
> crawshaw.io/sqlite to use the equivalent zombiezen.com/go/sqlite APIs.
>
> Try it out and let me know what you think!
>
> Thanks to Jan Mercl and David Crawshaw for early feedback and the amazing 
> packages this builds upon.
>
> -Ross
>
> --
> 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/3878d18f33bea1eb1d1805bd17705e612da337e8%40hey.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/CANODV3m-udUNKEwi%3D1Anmr1%3DX-beu5hrN%3Dw%3DhUYPHrrAE%3Daw_g%40mail.gmail.com.


Re: [go-nuts] HTTP request reading

2021-04-30 Thread Amit Saha


> On 29 Apr 2021, at 11:27 pm, K. Alex Mills  wrote:
> 
> Partial responses inline, HTH.
> 
> On Thu, Apr 29, 2021, 6:09 AM Amit Saha  <mailto:amitsaha...@gmail.com>> wrote:
> Hi all, when an incoming request comes in, does the ListenAndServe()
> function read the first line (as explained in
> https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages 
> <https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages>) to figure
> out whether there is a handler registered or not for the path and then
> simply hands it over to the handler (if there is one)?
> 
> In the http package, that role is played by the ServeMux 
> <https://golang.org/pkg/net/http/#ServeMux>.
> 
> 
> It seems like it is also reading the headers, and then populating the
> http.Request object with them. But, I am curious as to why does it do
> so? Is there any specific header that the server must look for?
> 
> HTTP headers arrive on the wire before the body, so they have to be consumed 
> prior to the body. The HTTP package parses them and makes them available so 
> the handler and middleware can make use of them as needed to do 
> header-related things like authentication.
> 
> 
> As far as the Body is concerned, that is left to the handler to
> decide, is that correct?
> 
> I guess you mean that the handler gets to decide for itself whether to read 
> the body or not. AFAIK, that's correct.
> 
> One benefit of parsing the headers into a map is that it makes HTTP 
> middleware more composeable and easier to write. Parsing the headers first 
> means different middlewares can examine the same headers. The same is not 
> true of the body, which can only be consumed once unless you add special 
> handling to store it in memory for later.
> 

Thank you for the replies, they do help clarify my understanding.


> 
> Thanks,
> Amit.
> 
> -- 
> 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 
> <mailto:golang-nuts%2bunsubscr...@googlegroups.com>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/CANODV3kSLPyV0snkPOr8Q_j%2BR%3DGOfVie2FNxEA1B-8%2BFvQxMEA%40mail.gmail.com
>  
> <https://groups.google.com/d/msgid/golang-nuts/CANODV3kSLPyV0snkPOr8Q_j%2BR%3DGOfVie2FNxEA1B-8%2BFvQxMEA%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/BDE1F30C-901D-4CE5-AB75-82A10499CDD3%40gmail.com.


[go-nuts] HTTP request reading

2021-04-29 Thread Amit Saha
Hi all, when an incoming request comes in, does the ListenAndServe()
function read the first line (as explained in
https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages) to figure
out whether there is a handler registered or not for the path and then
simply hands it over to the handler (if there is one)?

It seems like it is also reading the headers, and then populating the
http.Request object with them. But, I am curious as to why does it do
so? Is there any specific header that the server must look for?

As far as the Body is concerned, that is left to the handler to
decide, is that correct?

Thanks,
Amit.

-- 
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/CANODV3kSLPyV0snkPOr8Q_j%2BR%3DGOfVie2FNxEA1B-8%2BFvQxMEA%40mail.gmail.com.


Re: [go-nuts] tests, init() and flags

2021-04-24 Thread Amit Saha
On Sun, Apr 25, 2021 at 7:10 AM Shivendra Singh
 wrote:
>
> Yes @Jan Mercl  absolutely correct!!
>
> Able to figure out a solution.
> Basically the go mod i am using as part of the code is doing something like 
> this :
> func init() {
> //reads vars
> flag.Parse()
> }
>
> The problem arises when doing flag.Parse() as part of the init.
> https://github.com/golang/go/issues/31859#issuecomment-489889428 gives very 
> valuable insights to the issue.
>
> High Level Problems that may occur :
> 1. if i am reading any command line args anywhere else it will fail, since in 
> this code flag.Parse() is happening during init()
> 2. The code snippet :
> var _ = func() bool {
> testing.Init()
> return true
> }()
> This somewhat of a hack to explicitly invoke init() method and only helps in 
> a very few cases.
>
> As a solution, i went ahead to remove the flag.Parse() from init() because it 
> is really wrong and rather call it from my main().
> Just doing this change unblocked me for testing as now from test method i can 
> easily invoke a setup call to do flag.Parse() as Jan Mercl pointed out.

I would also recommend that you create a new FlagSet object
(https://golang.org/pkg/flag/#NewFlagSet) and use that to write more
testable applications. So, for example:

func setupFlagsAndParse(args []string) {
fs := flag.NewFlagSet(..)
# setup the flag and options as usual
fs.Parse(args)
   ...
}

Then, you call this function from main() as
setupFlagsAndParse(os.Args[1:]) or your tests with any slice of
strings that you may want to test.

-- 
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/CANODV3m9tth_4OA3yDdg2JX6guVe8aj7EbCygwMBYcShUZGnhg%40mail.gmail.com.


Re: [go-nuts] Purpose of TimeoutHandler

2021-04-24 Thread Amit Saha


> On 19 Apr 2021, at 10:07 pm, Amit Saha  wrote:
> 
> Thank you, I was suspecting that this might be the way to do it.

This is another approach I thought would also work without using channels or 
where you are just running a sequence of steps in a handler:

func handleUserAPI(w http.ResponseWriter, r *http.Request) {
log.Println("I started processing the request")
time.Sleep(15 * time.Second)

log.Println("Before continuing, i will check if the timeout has already 
expired")
if r.Context().Err() != nil {
log.Printf("Aborting further processing: %v\n", 
r.Context().Err())
return
}
fmt.Fprintf(w, "Hello world!")
log.Println("I finished processing the request")
}




> 
> 
> 
> 
> On Mon, 19 Apr 2021, 6:02 pm Brian Candler,  <mailto:b.cand...@pobox.com>> wrote:
> Your inner request handler needs to use the request context to cancel its 
> work.
> 
> package main
> 
> import (
>   "log"
>   "net/http"
>   "time"
> )
> 
> type foo struct{}
> 
> func (f foo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
>   log.Print("New request")
>   for i := 0; i < 10; i++ {
>   select {
>   case <-r.Context().Done():
>   log.Print("Aborted")
>   return
>   case <-time.After(1 * time.Second):
>   log.Print("Tick")
>   }
>   w.Write([]byte(".\n"))
>   }
>   w.Write([]byte("hello world\n"))
>   log.Print("Completed")
> }
> 
> func main() {
>   fooHandler := foo{}
>   timeoutHandler := http.TimeoutHandler(fooHandler, 5*time.Second, "Too 
> slow!\n")
>   http.Handle("/foo", timeoutHandler)
>   log.Fatal(http.ListenAndServe(":8080", nil))
> }
> 
> To test:
> curl localhost:8080/foo
> 
> -- 
> 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 
> <mailto:golang-nuts+unsubscr...@googlegroups.com>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/d75657a2-5ab5-4908-9997-3fe3dfe3b87an%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/golang-nuts/d75657a2-5ab5-4908-9997-3fe3dfe3b87an%40googlegroups.com?utm_medium=email_source=footer>.

-- 
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/40B6DCC5-A321-4AF8-A03B-F5A0CFD18989%40gmail.com.


Re: [go-nuts] Re: Purpose of TimeoutHandler

2021-04-19 Thread Amit Saha
Thank you, I was suspecting that this might be the way to do it.




On Mon, 19 Apr 2021, 6:02 pm Brian Candler,  wrote:

> Your inner request handler needs to use the request context to cancel its
> work.
>
> package main
>
> import (
> "log"
> "net/http"
> "time"
> )
>
> type foo struct{}
>
> func (f foo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
> log.Print("New request")
> for i := 0; i < 10; i++ {
> select {
> case <-r.Context().Done():
> log.Print("Aborted")
> return
> case <-time.After(1 * time.Second):
> log.Print("Tick")
> }
> w.Write([]byte(".\n"))
> }
> w.Write([]byte("hello world\n"))
> log.Print("Completed")
> }
>
> func main() {
> fooHandler := foo{}
> timeoutHandler := http.TimeoutHandler(fooHandler, 5*time.Second, "Too
> slow!\n")
> http.Handle("/foo", timeoutHandler)
> log.Fatal(http.ListenAndServe(":8080", nil))
> }
>
> To test:
> curl localhost:8080/foo
>
> --
> 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/d75657a2-5ab5-4908-9997-3fe3dfe3b87an%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/CANODV3k9zHPpQaBMj5wbobJP-Jgaz00Yw9rBJFnz6--cG6DZ8Q%40mail.gmail.com.


[go-nuts] Purpose of TimeoutHandler

2021-04-18 Thread Amit Saha
Hi all - I wrongly assumed that the TimeoutHandler() is supposed to help 
application authors free up resources on the sever for anything running than 
the expected duration of time. However that doesn’t seem to be the case. The 
inner handler continues running, only the client gets a 503 after the duration 
is expired.

So, I guess have two queries:

1. What is the purpose of TimeoutHandler? Is it to just let the client know 
that their request couldn’t be completed and they should just try again?
2. Is there currently another standard library provided solution to wrap a 
handler which will terminate the inner handler function when the duration 
expires?

Thanks,
Amit.


-- 
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/BC4BEA18-F07D-4597-88B6-B0B5395BEE5E%40gmail.com.


Re: [go-nuts] Go 1.16 and modules

2021-03-28 Thread Amit Saha
On Mon, Mar 29, 2021 at 12:15 PM Reto  wrote:
>
> On Mon, Mar 29, 2021 at 11:18:28AM +1100, Amit Saha wrote:
> > "Module-aware mode is enabled by default, regardless of whether a
> > go.mod file is present in the current working directory or a parent
> > directory. More precisely, the GO111MODULE environment variable now
> > defaults to on. To switch to the previous behavior, set GO111MODULE to
> > auto. "
> > Is that implying the above behavior?
>
> Yes: https://golang.org/ref/mod#mod-commands
>
> Note that you can still run the hello world programs via `go run` without
> having a module.

Thanks.

>
> --
> 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/20210329011453.3dddpz3syc6wv636%40feather.localdomain.

-- 
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/CANODV3kx-%2BeP%3DjGYDMv0g%2BrNZ5vcrCpOrmc9mit0FJj-x2iFFw%40mail.gmail.com.


[go-nuts] Go 1.16 and modules

2021-03-28 Thread Amit Saha
Hi Folks,

I just realized that with Go 1.16, you must create a module via go mod
init, even if you are writing a simple hello world program.

The most surprising aspect to me was that I can only know that's the
case if I don't have a go.mod file anywhere in the parent directory
chain. It becomes quite a bit of a surprise element for folks writing
materials for others to follow.

Was this change documented in the release notes? We do  have this:

"Module-aware mode is enabled by default, regardless of whether a
go.mod file is present in the current working directory or a parent
directory. More precisely, the GO111MODULE environment variable now
defaults to on. To switch to the previous behavior, set GO111MODULE to
auto. "

Is that implying the above behavior?

-- 
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/CANODV3m%2BLCDQem%2Bewny29SHXxvH-TtzW2zHuJ9-08TFX%3DSmYKw%40mail.gmail.com.


Re: [go-nuts] Use cases for custom ResponseWriter

2021-03-18 Thread Amit Saha
On Wed, Mar 17, 2021 at 8:54 PM Amnon  wrote:
>
>
> https://github.com/gorilla/handlers
> contains a number of ResponseWriters to provide functions such as compression 
> and logging
> On Wednesday, 17 March 2021 at 08:41:09 UTC be...@pferdewetten.de wrote:
>>
>> net/http/httptest contains an implementation of http.ResponseWriter that's 
>> used for testing request handlers in isolation (without spinning up a 
>> server).

Thanks both of you!

>>
>> On 17.03.21 03:12, Amit Saha wrote:
>>
>> Hi all,
>>
>> I came across the need for writing a custom ResponseWriter when I
>> wanted to log the response status code.
>>
>> Is there any other use case where this is needed to implement one?
>> Thanks and appreciate any pointers.
>>
>> Best Regards,
>> Amit.
>>
>> --
>> Gregor Best
>>   be...@pferdewetten.de
>
> --
> 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/5261f1fb-5bba-4c11-b650-304aea20427cn%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/CANODV3%3D2Ra64PCdva6d6oPxsQ%2B8kC12zAi42UGFP98_NxZ7sUw%40mail.gmail.com.


[go-nuts] Use cases for custom ResponseWriter

2021-03-16 Thread Amit Saha
Hi all,

I came across the need for writing a custom ResponseWriter when I
wanted to log the response status code.

Is there any other use case where this is needed to implement one?
Thanks and appreciate any pointers.

Best Regards,
Amit.

-- 
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/CANODV3%3D7OaP5jcy07eZWzgBg1NuNw0p3CpKokfOpMu3qkLYf%3DQ%40mail.gmail.com.


Re: [go-nuts] Error while executing exec.command

2021-03-15 Thread Amit Saha
On Tue, 16 Mar 2021, 9:37 am Sharan Guhan,  wrote:

> Hi Experts,
>
> I am relatively new to GOLANG and trying a simple program as below which
> is failing in fetching the output of a file, which works otherwise on the
> command prompt:
>
> Lang : GOLANG
> OS : Linux/Centos
> Problem : Using exec.command to get the output of a certain file
> Error: Getting no such file or directory error:
>
> command = fmt.Sprintf("cat /sys/bus/pci/devices/%s.0/numa_node",
> name)
>
> *2021/03/15 11:55:59 fork/exec cat
> /sys/bus/pci/devices/:19:00.0/numa_node: no such file or directory*
> *exit status 1*
>
> However, the same executed on linux prompt works from the same working
> directory as the code :
> *[root-v05 ~]# cat /sys/bus/pci/devices/:19:00.0/numa_node*
> *0*
> [root-v05 ~]#
>
>
> Please suggest what I may be missing.
>

The exec.Command() expects the arguments to be passed separately. The
problem you are having here is that it is thinking your entire command
along with the arguments is assumed to be the command to execute.

I would have a look at the documentation for exec.Command



> Sharan
>
>
>
> --
> 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/c5f6d10b-e857-4652-ac94-0e200233db9dn%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/CANODV3n5WzwQ8j7bmCjruGU%3DbG%2BXdE1-mSQBxrx0O8CfcaJvLw%40mail.gmail.com.


Re: [go-nuts] still struggling to understand modules

2021-02-23 Thread Amit Saha
On Wed, Feb 24, 2021 at 12:49 PM rob  wrote:
>
> Hi.  I'm a hobby programmer.  I've been able to learn and use gopath
> mode for a while.  I've accumulated 20+ programs that I use that were
> build using gopath mode.  I'm not using a package manager, as all my
> code is in GOPATH, ie, ~/go/src/
>
> I don't understand how to convert this to modules.  I've read the blogs
> on golang.org/ref/mod# ... , and still don't understand how to convert
> to modules.
>
> I tried a year or so ago, but I then could not compile anything, so I
> nuked whatever I did and reverted to GOPATH.
>
> I'm looking for a conversion guide for the complete idiot, or for
> dummies if that's preferable.
>
> I'd be satisfied if there was a step by step guide for converting
> ~/go/src/hello.go to modules.  I could probably adapt that.
>
> Right now, all documentation I've seen starts by doing git clone
> https://github.com/my-own-code.  And then I'm lost.
>
> If this is not the correct forum to post this very basic request for
> help, guidance as to where else I should GO would be helpful.

What is your current Go version?

You mention:

~/go/src/hello.go

Is that the exact path or do you have ~/go/src//hello.go?

When you say you have 20+ programs that I use that were build using
gopath mode, how is the source structure exactly on your system?




>
> --rob solomon
>
>
> --
> 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/24b2ccb9-f4f5-83b0-6a1b-6774021ce029%40gmail.com.



--
Software Engineer, Author
https://echorand.me

-- 
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/CANODV3m5UM-G2PKW6m5P6YXwEXNPbR_Gg%3D-VZqWX%3D8ntOGTP1Q%40mail.gmail.com.


Re: [go-nuts] Clarification related to HTTP request context cancellations and goroutines

2021-01-08 Thread Amit Saha



> On 8 Jan 2021, at 1:59 pm, Robert Engels  wrote:
> 
> You need to pass the context to the expensive work a periodically check if it 
> has been cancelled. 

Thanks. I was thinking how to implement this. Is this a nice way to do it?

func clientDisconnected(ctx context.Context, done chan bool) bool {
select {
case <-done:
return false
case <-ctx.Done():
log.Printf("api: client disconnected.")
return true
}
}


func apiHandlerFunction(w http.ResponseWriter, r *http.Request) {
done := make(chan bool)
go func() {
log.Println("First expensive operation")
time.Sleep(5 * time.Second)
done <- true
}()

if clientDisconnected(r.Context(), done) {
return
}

go func() {
log.Println("Second expensive operation")
time.Sleep(5 * time.Second)
done <- true
}()

if clientDisconnected(r.Context(), done) {
return
}

fmt.Fprintf(w, "All operations done")
}

> 
>> On Jan 7, 2021, at 8:55 PM, Amit Saha  wrote:
>> 
>> Hi all, I want to confirm whether my understanding related to handling
>> HTTP client disconnections is correct or not.
>> 
>> Consider this:
>> 
>> func longRunningProcessing(ctx context.Context, w http.ResponseWriter) {
>>   done := make(chan bool)
>>   go func() {
>>   expensiveWork() # This is a blocking expensive processing call
>>   done <- true
>>  }()
>>   select {
>>   case <-ctx.Done():
>> log.Printf("Client disconnected. Cancelling expensive operation.")
>> return
>>case <- done:
>> fmt.Fprintf(w, "Done!")
>>   }
>> }
>> 
>> func apiHandler(w http.ResponseWriter, r *http.Request) {
>>   ctx := r.Context()
>>   longRunningProcessing(ctx)
>> }
>> 
>> When a client connection close is detected, the Done() call in select
>> unblocks and the call goes out of the longRunningProcessing()
>> function.
>> However, the goroutine with the expensiveWork() is actually still running?
>> 
>> It seems like there is *no* way there is *not* going to be a goroutine
>> leak (and resources associated with it) in a pattern like the above
>> unless
>> I have control over expensiveWork() and can instrument it with a child
>> context. I am also thinking if there is a way to obtain the behavior
>> similar to exec.CommandContext() command
>> which does a hard kill of the external process when the context is cancelled.
>> 
>> Thanks for any insights and clarifications.
>> 
>> -- 
>> 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/CANODV3k9gF0gLWB1Z1OGaH-ArPbq8mNPKx9mzrTOTCa-kZZmvg%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/76D2DEE6-9FB0-4DC9-AAF5-2744BB6A1BAE%40gmail.com.


[go-nuts] Re: HTTP RequestWithContext in a server and error handling pattern

2021-01-08 Thread Amit Saha



> On 9 Jan 2021, at 9:50 am, Amit Saha  wrote:
> 
> Say I have created a RequestWithContext:
> 
> r, err := http.NewRequestWithContext(ctx, "GET", 
> "http://127.0.0.1:8080/healthcheck;, nil)
> 
> Now, I make the request in the context of *another* HTTP request handling 
> function:
> 
> Func myHandler(w http.ResponseWriter, r *http.Request) {
> ...
> 
> resp, err := client.Do(r)
> if err != nil {
>   http.Error(w, err.Error(), http.StatusInternalServerError)
> }
>  return
> }
> …
> }
> 
> 
> When I cancel this context, the above http.Error() call triggers a 
> superfluous write header message.
> I dug down a bit and I can see that when the context is cancelled, the 
> finishRequest() function 
> (https://golang.org/src/net/http/server.go?h=finishRequest#L1613) is called 
> which essentially writes a 200 status code. Hence, when the code above goes 
> to write an error, it gets the superfluous writeHeader message.
> 
> I then updated my code above to be:
> 
> resp, err := client.Do(r)
> if err != nil {
>   if err != context.Canceled {
>   http.Error(w, err.Error(), http.StatusInternalServerError)
>   }
> return
> }
> 
> Wondering if anyone has any thoughts on this pattern and if there is any 
> reason why we complete the full lifecycle of finsihRequest() on context 
> cancellation even if it is going to fail somewhere down the line since in 
> this case, the client has already disconnected?

I missed the complete handler function which will make the query more sensible:

func getData(ctx context.Context, w http.ResponseWriter) {
done := make(chan bool)
client := http.Client{}

go func() {
r, err := http.NewRequestWithContext(ctx, "GET", 
"http://127.0.0.1:8080/healthcheck;, nil)
if err != nil {
http.Error(w, err.Error(), 
http.StatusInternalServerError)
return
}
resp, err := client.Do(r)
if err != nil && err != context.Canceled {
http.Error(w, err.Error(), 
http.StatusInternalServerError)
}
return
}
defer resp.Body.Close()
io.Copy(w, resp.Body)
done <- true
}()

select {
case <-done:
return

case <-ctx.Done():
log.Printf("api: client disconnected.")
return # This return triggers the finishRequest() function call 
to be made
}
}




-- 
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/73125598-E476-41BC-9A37-B20958448372%40gmail.com.


[go-nuts] HTTP RequestWithContext in a server and error handling pattern

2021-01-08 Thread Amit Saha
Say I have created a RequestWithContext:

r, err := http.NewRequestWithContext(ctx, "GET", 
"http://127.0.0.1:8080/healthcheck;, nil)

Now, I make the request in the context of *another* HTTP request handling 
function:

Func myHandler(w http.ResponseWriter, r *http.Request) {
...

resp, err := client.Do(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
  return
}
…
}


When I cancel this context, the above http.Error() call triggers a superfluous 
write header message.
I dug down a bit and I can see that when the context is cancelled, the 
finishRequest() function 
(https://golang.org/src/net/http/server.go?h=finishRequest#L1613) is called 
which essentially writes a 200 status code. Hence, when the code above goes to 
write an error, it gets the superfluous writeHeader message.

I then updated my code above to be:

resp, err := client.Do(r)
if err != nil {
if err != context.Canceled {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
 return
}

Wondering if anyone has any thoughts on this pattern and if there is any reason 
why we complete the full lifecycle of finsihRequest() on context cancellation 
even if it is going to fail somewhere down the line since in this case, the 
client has already disconnected?

-- 
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/FECFD0DC-85F3-4C4A-95E1-52C7F9E9A18F%40gmail.com.


[go-nuts] Clarification related to HTTP request context cancellations and goroutines

2021-01-07 Thread Amit Saha
Hi all, I want to confirm whether my understanding related to handling
HTTP client disconnections is correct or not.

Consider this:

func longRunningProcessing(ctx context.Context, w http.ResponseWriter) {
done := make(chan bool)
go func() {
expensiveWork() # This is a blocking expensive processing call
done <- true
   }()
select {
case <-ctx.Done():
  log.Printf("Client disconnected. Cancelling expensive operation.")
  return
 case <- done:
  fmt.Fprintf(w, "Done!")
}
}

func apiHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
longRunningProcessing(ctx)
}

When a client connection close is detected, the Done() call in select
unblocks and the call goes out of the longRunningProcessing()
function.
However, the goroutine with the expensiveWork() is actually still running?

It seems like there is *no* way there is *not* going to be a goroutine
leak (and resources associated with it) in a pattern like the above
unless
I have control over expensiveWork() and can instrument it with a child
context. I am also thinking if there is a way to obtain the behavior
similar to exec.CommandContext() command
which does a hard kill of the external process when the context is cancelled.

Thanks for any insights and clarifications.

-- 
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/CANODV3k9gF0gLWB1Z1OGaH-ArPbq8mNPKx9mzrTOTCa-kZZmvg%40mail.gmail.com.


Re: [go-nuts] Clarification in the description of context.WithValue()

2021-01-06 Thread Amit Saha
On Thu, Jan 7, 2021 at 10:20 AM Axel Wagner
 wrote:
>
> Hi,
>
> this works fine, but it incurs an extra allocation (AFAIK both for storing 
> the data, and for looking it up). requestContext is not zero-sized, so to 
> store it in an interface{}, a new value must be allocated and put into the 
> interface. This also has to happen on lookup (I don't think the compiler 
> optimizes that, today), because you need to convert it to interface{} when 
> passing it to `Value`. The recommended way
>
> type ctxKey struct{}
>
> type requestContext struct {
> requestID string
> requestID
> }
>
> ctx := context.WithValue(req.Context(), ctxKey{}, 
> requestContext{"request-123-abc", "client-451"})
>
> does not have this drawback - zero-sized values are special, because there 
> can only be one of them, so the compiler is free to re-use the same address. 
> So, a conversion of a zero-sized value to ctxKey{} is free.
> I would also argue, that the code is clearer and safer that way - because 
> there can only be one value of type ctxKey{}, you don't have to wonder what 
> would happen if you use different keys of the same type - and fmt.Println 
> only prints the type-name of keys, not the actual value 
> (https://play.golang.org/p/pAj6RclqAGw), so if you encounter a bug with 
> unexpected context-contents, it's just one less thing to worry about.
>
> So, yes, your way absolutely works. I would still strongly recommend going 
> the "official" route. It's one line of additional overhead, but there are 
> concrete technical benefits (as well as just the general benefits of 
> uniformity).

Your previous reply made me think of the same approach - use a
separate type for key.

> I would also argue, that the code is clearer and safer that way - because 
> there can only be one value of type ctxKey{}, you don't have to wonder what 
> would happen if you use different keys of the same type

I agree, I was thinking the same.

Thank you.



>
>
> On Thu, Jan 7, 2021 at 12:09 AM Amit Saha  wrote:
>>
>> On Thu, Jan 7, 2021 at 10:01 AM Axel Wagner
>>  wrote:
>> >
>> > Why do you need fields? If you need fields, you are likely putting too 
>> > many things into your context (note, that the lookup is O(N), with N being 
>> > the number of distinct keys - so you want the number of distinct keys to 
>> > be small). The intended use of `context.WithValue` is to only have one key 
>> > per "use-case" (normally, that means one key per package) and then store 
>> > the actual data in the value.
>> >
>> > So, yes, you *can* have fields and if you have fields and re-use one type 
>> > for many keys that way. And if you do that, you need to specify the exact 
>> > field values on the lookup as well. But if you want "many keys" from a 
>> > single type, that's a code-smell and I would suggest to change your 
>> > design. If you think you need it, feel free to expand on your use-case and 
>> > we might come up with a better solution :)
>>
>> This is what I have now in a HTTP handler function:
>>
>> Attach the data:
>>
>> c := requestContext{
>>  requestID: "request-123-abc",
>>  clientID: "client-451",
>> }
>>
>> currentCtx := req.Context()
>> newCtx := context.WithValue(currentCtx, requestContext{}, c)
>> req = req.WithContext(newCtx)
>>
>>
>> Retrieve the data somewhere else during processing the request:
>>
>> ctx := req.Context()
>> v := ctx.Value(requestContext{})
>> if m, ok := v.(requestContext); ok {
>>  log.Printf("Processing request: %v", m)
>> }
>>
>> Is this inefficient?
>>
>>
>>
>>
>> >
>> >
>> > On Wed, Jan 6, 2021 at 11:52 PM Amit Saha  wrote:
>> >>
>> >> On Thu, Jan 7, 2021 at 9:48 AM Axel Wagner
>> >>  wrote:
>> >> >
>> >> >
>> >> >
>> >> > On Wed, Jan 6, 2021 at 11:39 PM Amit Saha  wrote:
>> >> >>
>> >> >> Is it fair to say then that any user-defined type here is acceptable 
>> >> >> as a key?
>> >> >
>> >> >
>> >> > No. You can create a user-defined type out of *any* type T, by just 
>> >> > writing `type X T`. In particular, you can write `type X func()` and 
>> >> > get an incomparable user-defined type.
>> >> >
>> >> > I highly recommend sticking to the recommendation from the docs: Use 
>> >> > `typ

Re: [go-nuts] Clarification in the description of context.WithValue()

2021-01-06 Thread Amit Saha
On Thu, Jan 7, 2021 at 10:01 AM Axel Wagner
 wrote:
>
> Why do you need fields? If you need fields, you are likely putting too many 
> things into your context (note, that the lookup is O(N), with N being the 
> number of distinct keys - so you want the number of distinct keys to be 
> small). The intended use of `context.WithValue` is to only have one key per 
> "use-case" (normally, that means one key per package) and then store the 
> actual data in the value.
>
> So, yes, you *can* have fields and if you have fields and re-use one type for 
> many keys that way. And if you do that, you need to specify the exact field 
> values on the lookup as well. But if you want "many keys" from a single type, 
> that's a code-smell and I would suggest to change your design. If you think 
> you need it, feel free to expand on your use-case and we might come up with a 
> better solution :)

This is what I have now in a HTTP handler function:

Attach the data:

c := requestContext{
 requestID: "request-123-abc",
 clientID: "client-451",
}

currentCtx := req.Context()
newCtx := context.WithValue(currentCtx, requestContext{}, c)
req = req.WithContext(newCtx)


Retrieve the data somewhere else during processing the request:

ctx := req.Context()
v := ctx.Value(requestContext{})
if m, ok := v.(requestContext); ok {
 log.Printf("Processing request: %v", m)
}

Is this inefficient?




>
>
> On Wed, Jan 6, 2021 at 11:52 PM Amit Saha  wrote:
>>
>> On Thu, Jan 7, 2021 at 9:48 AM Axel Wagner
>>  wrote:
>> >
>> >
>> >
>> > On Wed, Jan 6, 2021 at 11:39 PM Amit Saha  wrote:
>> >>
>> >> Is it fair to say then that any user-defined type here is acceptable as a 
>> >> key?
>> >
>> >
>> > No. You can create a user-defined type out of *any* type T, by just 
>> > writing `type X T`. In particular, you can write `type X func()` and get 
>> > an incomparable user-defined type.
>> >
>> > I highly recommend sticking to the recommendation from the docs: Use `type 
>> > ctxKey struct{}` as a key type and `ctxKey{}` as the key - it's comparable 
>> > and the only value equal to `ctxKey{}` is `ctxKey{}`, which gives exactly 
>> > the desirable semantics. And as it's zero-sized, it also doesn't occur any 
>> > allocation (at least with gc, all zero-sized objects have the same 
>> > address). You could also use `[0]int` or similar, but `struct{}` is very 
>> > common for use-cases like this.
>>
>> Thanks. If I wanted to have a struct like this
>>
>> type myReqContext struct {
>> ID1 string
>> ID2 string
>> }
>>
>> Would the correct key and value be: myReqContext{}, myReqContext{ID1:
>> "abcd", ID2: "defg"}?
>>
>> The key isn't zero value any more, or is it? If so, should I have a
>> dedicated empty struct for the key?
>>
>>
>>
>> >
>> >>
>> >>
>> >>
>> >> >>
>> >> >> Thanks,
>> >> >> Amit.
>> >> >>
>> >> >> --
>> >> >> 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/CANODV3kUmtJvG9Lfys_HXfedwnm54neA6uub6cY1aiz%2B9w5DUA%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/CANODV3%3Diq%3Dm1xjDV%2Bo8PaJHp49CRkipKbkhSJMKLC6UEJ_zXCA%40mail.gmail.com.


Re: [go-nuts] Clarification in the description of context.WithValue()

2021-01-06 Thread Amit Saha
On Thu, Jan 7, 2021 at 9:48 AM Axel Wagner
 wrote:
>
>
>
> On Wed, Jan 6, 2021 at 11:39 PM Amit Saha  wrote:
>>
>> Is it fair to say then that any user-defined type here is acceptable as a 
>> key?
>
>
> No. You can create a user-defined type out of *any* type T, by just writing 
> `type X T`. In particular, you can write `type X func()` and get an 
> incomparable user-defined type.
>
> I highly recommend sticking to the recommendation from the docs: Use `type 
> ctxKey struct{}` as a key type and `ctxKey{}` as the key - it's comparable 
> and the only value equal to `ctxKey{}` is `ctxKey{}`, which gives exactly the 
> desirable semantics. And as it's zero-sized, it also doesn't occur any 
> allocation (at least with gc, all zero-sized objects have the same address). 
> You could also use `[0]int` or similar, but `struct{}` is very common for 
> use-cases like this.

Thanks. If I wanted to have a struct like this

type myReqContext struct {
ID1 string
ID2 string
}

Would the correct key and value be: myReqContext{}, myReqContext{ID1:
"abcd", ID2: "defg"}?

The key isn't zero value any more, or is it? If so, should I have a
dedicated empty struct for the key?



>
>>
>>
>>
>> >>
>> >> Thanks,
>> >> Amit.
>> >>
>> >> --
>> >> 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/CANODV3kUmtJvG9Lfys_HXfedwnm54neA6uub6cY1aiz%2B9w5DUA%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/CANODV3mDhTeORcYP-jzGm2Ckjkp37-mcMX6ich1SWtwDAnAWjQ%40mail.gmail.com.


Re: [go-nuts] Clarification in the description of context.WithValue()

2021-01-06 Thread Amit Saha
On Thu, Jan 7, 2021 at 9:33 AM Axel Wagner
 wrote:
>
> On Wed, Jan 6, 2021 at 11:31 PM Amit Saha  wrote:
>>
>> In the description of context.WithValue(), we have:
>>
>> The provided key must be comparable and should not be of type string
>> or any other built-in type to avoid collisions between packages using
>> context. Users of WithValue should define their own types for keys. To
>> avoid allocating when assigning to an interface{}, context keys often
>> have concrete type struct{}.
>>
>> I am wondering if someone can explain what exactly comparable means here?
>>
>> In other languages and Go
>> (https://golang.org/ref/spec#Comparison_operators), comparable usually
>> means being able to compare two values for equality/greater/lesser.
>
>
> The linked spec section actually contradicts that:
>
>> The equality operators == and != apply to operands that are comparable. The 
>> ordering operators <, <=, >, and >= apply to operands that are ordered.
>
>
> (emphasis from the spec). So, the definition of "comparable" is "can be used 
> with == and !=" and what exactly that means is listed below that quote.
>
> Hope that helps
>

Thanks for pointing it out. I missed that - we have two things here,
ordered and comparison here then.

Is it fair to say then that any user-defined type here is acceptable as a key?


>>
>> Thanks,
>> Amit.
>>
>> --
>> 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/CANODV3kUmtJvG9Lfys_HXfedwnm54neA6uub6cY1aiz%2B9w5DUA%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/CANODV3n7WUgrU82JygSJ30LiyiJr_Fmbn0L4mQ5s-7NNZbm3Jg%40mail.gmail.com.


[go-nuts] Clarification in the description of context.WithValue()

2021-01-06 Thread Amit Saha
In the description of context.WithValue(), we have:

The provided key must be comparable and should not be of type string
or any other built-in type to avoid collisions between packages using
context. Users of WithValue should define their own types for keys. To
avoid allocating when assigning to an interface{}, context keys often
have concrete type struct{}.

I am wondering if someone can explain what exactly comparable means here?

In other languages and Go
(https://golang.org/ref/spec#Comparison_operators), comparable usually
means being able to compare two values for equality/greater/lesser.

Thanks,
Amit.

-- 
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/CANODV3kUmtJvG9Lfys_HXfedwnm54neA6uub6cY1aiz%2B9w5DUA%40mail.gmail.com.


Re: [go-nuts] json.NewDecoder()/Decode() versus Unmarshal() for single large JSON objects

2020-12-29 Thread Amit Saha
On Wed, Dec 30, 2020 at 4:07 AM Amnon  wrote:
>
> How do you know, if you don't check? FTR, it's not just about sending 
> garbage, it's also about requests accidentally being truncated or just 
> generally garbled.
>
> json.NewDecoder(r.Body).Decode() will return an error if the request 
> is garbled or truncated.
>
> The other nice thing about this code is that it composes nicely  with the 
> context.
> net/http provides the body as a Reader. And json NewDecoder takes a reader. 
> The two fit together nicely, without any need
> for adapters.

This is a good point, I like that viewpoint.

-- 
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/CANODV3%3D0DOQsBs_EWyTk05ZJk1g0TDj2XwM3GKPBdJtQWvhrTQ%40mail.gmail.com.


Re: [go-nuts] json.NewDecoder()/Decode() versus Unmarshal() for single large JSON objects

2020-12-29 Thread Amit Saha
On Tue, Dec 29, 2020 at 9:50 PM Axel Wagner
 wrote:
>
> There is an important semantic difference between the two, which means you 
> almost never want to use a `Decoder`: `Unmarshal` is for parsing a single 
> json document, whereas a `Decoder` is for parsing a stream of concatenated 
> documents, like so: https://play.golang.org/p/4uiNyJlNIKh. In particular, 
> this means that using a `Decoder` silently drops trailing data and might not 
> detect erronous json: https://play.golang.org/p/cuOAUnKCuEk
> So, unless you specifically know that you have a stream of concatenated json 
> documents `Decoder` is not actually doing what you want.

Indeed, thank you for the explanation.

>
> On Tue, Dec 29, 2020 at 11:37 AM Amit Saha  wrote:
>>
>> On Tue, Dec 29, 2020 at 11:35 AM burak serdar  wrote:
>> >
>> > On Mon, Dec 28, 2020 at 5:22 PM Amit Saha  wrote:
>> > >
>> > > Hi all, let's say I am a single large JSON object that I want to
>> > > process in my HTTP server backend.
>> > >
>> > > I am trying to get my head around if there is any performance
>> > > advantage - memory or CPU to use the json.NewDecoder()/Decode()
>> > > mechanism versus the Unmarshal() function?
>> >
>> > Unmarshal uses the decoder for unmarshaling. Unless you are planning
>> > to process the JSON object piece by piece using a decoder, the two are
>> > identical in terms of performance.
>>
>> Thank you for confirming.
>>
>>
>> >
>> > >
>> > > Thanks,
>> > > Amit
>> > >
>> > > --
>> > > 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/CANODV3%3DU2KRRkvAAEfYqRtCVtYnh2dmGreqePF8QXLo1PriSPw%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/CANODV3%3DpXGcHzhSmaLr3f911i2CCYb0_j8CGH7zKAZXd_xTD-A%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/CANODV3mFGGetoijZAXzrw9QLEji1asH1eWZbkVn8-TymVsTOOw%40mail.gmail.com.


Re: [go-nuts] json.NewDecoder()/Decode() versus Unmarshal() for single large JSON objects

2020-12-29 Thread Amit Saha
On Tue, Dec 29, 2020 at 11:35 AM burak serdar  wrote:
>
> On Mon, Dec 28, 2020 at 5:22 PM Amit Saha  wrote:
> >
> > Hi all, let's say I am a single large JSON object that I want to
> > process in my HTTP server backend.
> >
> > I am trying to get my head around if there is any performance
> > advantage - memory or CPU to use the json.NewDecoder()/Decode()
> > mechanism versus the Unmarshal() function?
>
> Unmarshal uses the decoder for unmarshaling. Unless you are planning
> to process the JSON object piece by piece using a decoder, the two are
> identical in terms of performance.

Thank you for confirming.


>
> >
> > Thanks,
> > Amit
> >
> > --
> > 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/CANODV3%3DU2KRRkvAAEfYqRtCVtYnh2dmGreqePF8QXLo1PriSPw%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/CANODV3%3DpXGcHzhSmaLr3f911i2CCYb0_j8CGH7zKAZXd_xTD-A%40mail.gmail.com.


[go-nuts] json.NewDecoder()/Decode() versus Unmarshal() for single large JSON objects

2020-12-28 Thread Amit Saha
Hi all, let's say I am a single large JSON object that I want to
process in my HTTP server backend.

I am trying to get my head around if there is any performance
advantage - memory or CPU to use the json.NewDecoder()/Decode()
mechanism versus the Unmarshal() function?

Thanks,
Amit

-- 
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/CANODV3%3DU2KRRkvAAEfYqRtCVtYnh2dmGreqePF8QXLo1PriSPw%40mail.gmail.com.


Re: [go-nuts] json objects

2020-12-25 Thread Amit Saha
On Sat, 26 Dec 2020, 10:25 am Hamsa Hesham,  wrote:

> package main
> import "encoding/json"
> import "log"
> import "fmt"
>
> type book struct {
> IDint
> title string
> pubDate   string
> authorstring
> genre string
> publisher string
> language  string
> }
> func main(){
> b := book{
> ID:1,
> title:   "Standard",
> pubDate:  "Standard",
> author: "Second-rate",
> genre: "Standard",
> publisher: "Standard",
> language: "Standard",
> }
>
> var jsonData []byte
> jsonData, err := json.Marshal(b)
> if err != nil {
> log.Println(err)
> }
> fmt.Println(string(jsonData))
>
> }
>
> The output is {"ID":1}  .. why?!!
>

Your struct fields will need to start with a capital letter to be
exportable into JSON. Since ID is the only field here, you only see that in
your output.


-- 
> 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/44b561be-30eb-48cb-ae0c-5d56f09e1c2cn%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/CANODV3krc-uo7FO%2BpFnmqYaBdiD4C1wPeFxo6NW_mt96Vt8gqQ%40mail.gmail.com.


Re: [go-nuts] assigning to an http request in a handler

2020-12-21 Thread Amit Saha
On Tue, Dec 22, 2020 at 3:49 AM zilto karamazov  wrote:
>
>
> Hello,
>
> I'm wondering if it is ok to do this ?
>
> 
> package main
>
> import (
> "context"
> "fmt"
> "log"
> "net/http"
>
> "github.com/gorilla/mux"
> )
>
> func main() {
> addr := "127.0.0.1:8080"
> r := mux.NewRouter()
>
> r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
> newReq := r.WithContext(context.WithValue(r.Context(), 0, 
> "bla"))
> *r = *newReq
> // set context
> })
>
> r.Use(func(next http.Handler) http.Handler {
> return http.HandlerFunc(func(w http.ResponseWriter, r 
> *http.Request) {
> next.ServeHTTP(w, r)
>
> ctx := r.Context()
> v, ok := ctx.Value(0).(string)
> if !ok {
> fmt.Println("could not find context value")
> } else {
> fmt.Println(v)
> }
> })
> })
>
> s := {
> Addr:addr,
> Handler: r,
> }
>
> if err := s.ListenAndServe(); err != nil && err != 
> http.ErrServerClosed {
> log.Fatalf("could not listen on %s : %v", addr, err)
> }
> }
> 

What is the goal you are trying to accomplish 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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CANODV3%3D_w3cKUSs9nUDNNwSDWDnbFQ2M7fq%2BA2LSq3-jh7vGAQ%40mail.gmail.com.


Re: [go-nuts] Re: 1.16 beta 1 - Getting an error trying to use the "embed" package

2020-12-18 Thread Amit Saha
On Sat, 19 Dec 2020, 5:45 am Ian Lance Taylor,  wrote:

> On Fri, Dec 18, 2020 at 4:50 AM Amit Saha  wrote:
> >
> > On Fri, 18 Dec 2020, 11:44 pm Volker Dobler, 
> wrote:
> >>
> >> Use
> >>
> >> import _ "embed"  // note the _
> >>
> >> Your code does not  use package embed. A comment does
> >> not qualify as usage. As yous must import it for //go:embed
> >> comments to work you have to use a "side-effects-only"-import.
> >
> >
> > Ah yes. This should be mentioned in the package documentation. Probably
> it is not.
>
> This is fairly likely to change before the final release.  See
> https://golang.org/issue/43217.
>

Thank you


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


Re: [go-nuts] Re: 1.16 beta 1 - Getting an error trying to use the "embed" package

2020-12-18 Thread Amit Saha
On Fri, 18 Dec 2020, 11:44 pm Volker Dobler, 
wrote:

> Use
>
> import _ "embed"  // note the _
>
> Your code does not  use package embed. A comment does
> not qualify as usage. As yous must import it for //go:embed
> comments to work you have to use a "side-effects-only"-import.
>

Ah yes. This should be mentioned in the package documentation. Probably it
is not.

Thank you.

>
> V.
>
> On Friday, 18 December 2020 at 13:38:10 UTC+1 amits...@gmail.com wrote:
>
>> Hi all, has anyone tried using the “embed” package in the 1.16 beta 1
>> release?
>>
>> My data.go file looks as:
>>
>> package main
>>
>> import "embed"
>>
>> //go:embed templates/main.go.tmpl
>> var tmplMainGo []byte
>>
>>
>> When I build the program, I get this:
>>
>> ~/go/bin/go1.16beta1 build
>> # github.com/amitsaha/go-embed
>> ./data.go:3:8: imported and not used: “embed”
>>
>>
>> Now, I understand the error, but what’s the fix? I blindly tried to
>> remove the import, but then I get:
>>
>> # github.com/amitsaha/go-embed
>> ./data.go:5:3: //go:embed only allowed in Go files that import “embed"
>>
>> What am I doing wrong?
>>
>> My test code is here: https://github.com/amitsaha/go-embed
>>
>> Thank you.
>>
>> Best Regards,
>> Amit.
>>
>>
>>
>> --
> 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/0552d6cc-93be-49f6-96c0-cd1679854460n%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/CANODV3k5zQA9z8tL8HAc42Kp2Sv6qwMC9KNP4L%2Bfg%2B0oGN44Yg%40mail.gmail.com.


[go-nuts] Go 1.16 Beta 1 - Clarification regarding module

2020-12-18 Thread Amit Saha
Hi all,

The release notes has this:

Module-aware mode is enabled by default, regardless of whether a go.mod file is 
present in the current working directory or a parent directory. More precisely, 
the GO111MODULE environment variable now defaults to on. To switch to the 
previous behavior, set GO111MODULE to auto.

I didn’t quite understand what that meant but I think I understand it now that 
we always have to initialise a module before we can build a package? So, before 
I could just create a main.go and happily build my program. But now I have to 
run `go mod init ` before I can build it?

$ ~/go/bin/go1.16beta1 build
go: cannot find main module; see 'go help modules’  

Thanks,
Amit

-- 
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/B858A936-BBDA-4A07-8C8D-ECB079A52B93%40gmail.com.


[go-nuts] 1.16 beta 1 - Getting an error trying to use the "embed" package

2020-12-18 Thread Amit Saha
Hi all, has anyone tried using the “embed” package in the 1.16 beta 1 release?

My data.go file looks as:

package main

import "embed"

//go:embed templates/main.go.tmpl
var tmplMainGo []byte


When I build the program, I get this:

 ~/go/bin/go1.16beta1 build 
# github.com/amitsaha/go-embed
./data.go:3:8: imported and not used: “embed”


Now, I understand the error, but what’s the fix? I blindly tried to remove the 
import, but then I get:

# github.com/amitsaha/go-embed
./data.go:5:3: //go:embed only allowed in Go files that import “embed"

What am I doing wrong?

My test code is here: https://github.com/amitsaha/go-embed

Thank you.

Best Regards,
Amit.



-- 
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/9D4EF5A9-9C67-4CF1-B443-CF05FD155D94%40gmail.com.


Re: [go-nuts] What are debian:buster-slim advantages vs alpine ones?

2020-12-15 Thread Amit Saha
On Wed, 16 Dec 2020, 12:38 pm Constantine Vassilev, 
wrote:

> All examples in Google Cloud Run for building
> Golang Docker images are based on Debian.
>
> FROM golang:alpine AS builder
> ...
> FROM debian:buster-slim
> ...
>
> What are debian:buster-slim  advantages vs alpine ones?
>

Besides the size of the images, one key difference would be the underlying
C library - Debian would be glibc and Alpine would be libmusl

So advantages would be if knowing that libc is available.

> --
> 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/2433a1dd-c3ea-4c5c-9f6e-d1595cef99f9n%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/CANODV3mkLNhSfkqTrH3osXtxuSqo1xjOCzfwMZ6Mqn-wmAaKaQ%40mail.gmail.com.


Re: [go-nuts] Purpose of pattern in muxEntry

2020-12-15 Thread Amit Saha


> On 16 Dec 2020, at 5:49 am, jake...@gmail.com  wrote:
> 
> I have no special knowledge of the code, but it looks like the reason is so 
> that ServeMux.es ,  which is a []muxEntry, can be 
> searched. See the latter half of `func (mux *ServeMux) match() 
> ` for example. 
> 
> That said, it may be possible to have ServeMux.m be a `map[string]Handler` 
> instead. If so, then my guess is that ServeMux.m and ServeMux.es 
>  both use muxEntry just to slightly simplify the code. 
> The assumption would be that the memory cost is negligible. But I could be 
> wrong, and maybe there is a case where  ServeMux.m must be a muxEntry, and 
> not just a Handler.

Thank you for sharing your insights. Yes, I was looking at the append..() code 
slightly below and I saw a usage of that there. 


> 
> On Monday, December 14, 2020 at 8:52:27 PM UTC-5 amits...@gmail.com 
>  wrote:
> Hi all, the ServerMux struct is currently defined as:
> 
> type ServeMux struct {
> // other fields
> m map[string]muxEntry
> }
> 
> 
> The muxEntry is then defined as:
> 
> type muxEntry struct { 
> h Handler 
> pattern string 
> }
> 
> Is there any reason for also storing the pattern in the muxEntry since it 
> *seems* like the lookup only happens using the key in the map of ServeMux?
> 
> Thanks for any insights.
> 
> 
> Best Regards,
> Amit
> 
> 
> 
> -- 
> 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/42544681-9f1f-4e76-a4da-52330ee1628en%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/8127E7CE-6A64-4B5A-A7C6-CB144A0C14BD%40gmail.com.


[go-nuts] Purpose of pattern in muxEntry

2020-12-14 Thread Amit Saha
Hi all, the ServerMux struct is currently defined as:

type ServeMux struct {
// other fields
m map[string]muxEntry
}


The muxEntry is then defined as:

type muxEntry struct { 
h Handler 
pattern string 
}

Is there any reason for also storing the pattern in the muxEntry since it 
*seems* like the lookup only happens using the key in the map of ServeMux?

Thanks for any insights.


Best Regards,
Amit


-- 
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/0e8090ca-de72-4a18-9c71-7d021f326ed7n%40googlegroups.com.


Re: [go-nuts] Production Support

2020-12-09 Thread Amit Saha
Hi - your message sounds interesting. Why not open source your code?

On Thu, 10 Dec 2020, 10:17 am jm...@tele-metron.com, 
wrote:

> I have developed an API server using GO. Started as a retirement project
> but now has actual company that is ready to replace their current
> technology. I am looking for a group or individual that may be interested
> in taking on the support of the code base as I start to take retirement
> seriously. Open to any discussions.
> Thanks
>
> --
> 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/14f32b2a-8a59-4094-b708-26d3fb2ef922n%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/CANODV3kpgDd10eU8s-sN6uUZpQxH41GjBZB5%2B1VoR06C8%3DrHfg%40mail.gmail.com.


Re: [go-nuts] When does net/http's Client.Do return io.EOF?

2020-12-09 Thread Amit Saha



> On 7 Dec 2020, at 10:58 pm, 'Axel Wagner' via golang-nuts 
>  wrote:
> 
> We recently had the same issue.
> 
> On Mon, Dec 7, 2020 at 11:58 AM Gregor Best  wrote:
> Hi!
> 
> We're using a 3rd party provider's API to handle some of our customer
> requests. Interaction with their API consists of essentially POST'ing
> a small XML document to them.
> 
>  From time to time, `net/http`'s `Client.Do` returns an `io.EOF`
> when sending the request. For now, the provider always reported
> those instances as "we didn't get your request".
> 
> Cursory search in various Github issues and a glance at the source
> of `net/http` seems to indicate that `io.EOF` is almost always
> caused by the server closing the connection, but the client not
> getting the "it's now closed" signal before it tries to re-use the
> connection.
> 
> That was what I concluded as well. I think it could theoretically also happen 
> if a new connection is opened and immediately closed by the server.
> 
> FWIW, `fasthttp`'s HTTP client implementation treats `io.EOF` as
> "this request needs to be retried", but I don't know how much that
> knowledge transfers to `net/http`.
> 
> I think `fasthttp` is behaving incorrectly - in particular, if it also does 
> so for POST requests (you mention that you use them). They are, in general, 
> not idempotent and there is a race where the client sends the request, the 
> server receives it and starts handling it (causing some observable 
> side-effects) but then dies before it can send a response, with the 
> connection being closed by the kernel. If the client retries that (at a 
> different backend, or once the server got restarted), you might end up with 
> corrupted state.
> 
> AIUI, `net/http` never assumes requests are retriable - even GET requests - 
> and leaves it up to the application to decide, whether a request can be 
> retried or not. Our solution was to verify that all our requests *can* be 
> retried and then wrapping the client call with retries.

Just wanted to point out here that the standard Transport has some retry 
behaviour by default (https://golang.org/pkg/net/http/#Transport):

> Transport only retries a request upon encountering a network error if the 
> request is idempotent and either has no body or has its Request.GetBody 
> defined. HTTP requests are considered idempotent if they have HTTP methods 
> GET, HEAD, OPTIONS, or TRACE; or if their Header map contains an 
> "Idempotency-Key" or "X-Idempotency-Key" entry. If the idempotency key value 
> is a zero-length slice, the request is treated as idempotent but the header 
> is not sent on the wire.




-- 
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/5AC3D229-BDFD-490A-93DA-3822C5BE80B0%40gmail.com.


Re: [go-nuts] Trying to understand HTTP connection pooling behaviour - new DNS lookup

2020-11-28 Thread Amit Saha


> On 28 Nov 2020, at 11:22 pm, b.ca...@pobox.com  wrote:
> 
> > My actual query was to learn how the removal of the broken connection is 
> > done.
> 
> Ah OK.  I'm not familiar with this code (net/http/transport.go), and you've 
> already shown that the PutIdleConn trace action isn't called.  Maybe this 
> comment is helpful:

No worries at all. The PutIdleConn is not called for HTTP/2 connections which I 
found to be the case for https://godoc.org , but when I used 
http://godoc.org  (in my example that I shared), it was 
called and the error was nil.
> 
> if pconn.isBroken() || tooOld {
> // If either persistConn.readLoop has marked 
> the connection
> // broken, but Transport.removeIdleConn has 
> not yet removed it
> // from the idle list, or if this persistConn 
> is too old (it was
> // idle too long), then ignore it and look 
> for another. In both
> // cases it's already in the process of being 
> closed.
> list = list[:len(list)-1]
> continue
> }
> 
> Otherwise someone else can answer more specifically.

No problems. I did start looking at the above isBroken() method, but sort of 
got lost.



> 
> -- 
> 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/613e3778-7892-4ab9-8cc8-6db1a3e8f966n%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/2FAC9A9D-BF72-4566-9981-CA74233E8917%40gmail.com.


Re: [go-nuts] Trying to understand HTTP connection pooling behaviour - new DNS lookup

2020-11-28 Thread Amit Saha


> On 28 Nov 2020, at 10:36 pm, b.ca...@pobox.com  wrote:
> 
> Unless you timestamp those log lines, I don't see any evidence that the DNS 
> query is done when the godoc server shuts down.  Could it not just be that 
> after the 2 second sleep, when it's time to make a new connection, it finds 
> that the pool is empty so has to do a lookup to establish a new connection?

Yes, that’s correct. I inferred the same. What I wanted to learn was to see the 
code where the detection that the remote server had gone/connection had gone 
bad and it was removed from the pool.
> 
> I've tested this myself, using your code and apache running on the local 
> server.
> 
> $ go run httpclient.go http://localhost:80/
> DNS Start Info: {Host:localhost}
> DNS Done Info: {Addrs:[{IP:127.0.0.1 Zone:}] Err: Coalesced:false}
> Got Conn: {Conn:0xc10070 Reused:false WasIdle:false IdleTime:0s}
> Put Idle Conn Error: 
> Resp protocol: "HTTP/1.1"
> Sleeping now
> 
> Got Conn: {Conn:0xc10070 Reused:true WasIdle:true IdleTime:2.000554686s}
> Put Idle Conn Error: 
> Resp protocol: "HTTP/1.1"
> Sleeping now
> << at this point I "systemctl stop apache2" in another terminal >>
> << nothing happens until the 2 seconds is up, and then I see: >>
> 
> DNS Start Info: {Host:localhost}
> DNS Done Info: {Addrs:[{IP:127.0.0.1 Zone:}] Err: Coalesced:false}
> Got error in making request: Get "http://localhost:80/": dial tcp 
> 127.0.0.1:80: connect: connection refused
> Sleeping now
> 
> DNS Start Info: {Host:localhost}
> DNS Done Info: {Addrs:[{IP:127.0.0.1 Zone:}] Err: Coalesced:false}
> Got error in making request: Get "http://localhost:80/": dial tcp 
> 127.0.0.1:80: connect: connection refused
> Sleeping now
> ^Csignal: interrupt
> 
> So as far as I can tell, the client tries to re-use the existing connection 
> from the pool, finds there isn't one (or it's broken), and therefore tries to 
> establish a new connection - which of course requires a lookup of the 
> hostname in the URL.

That’s correct and matches my understanding which is great. My actual query was 
to learn how the removal of the broken connection is done.

> 
> Aside: I'd say the trace hooks "DNSStart" and "DNSDone" are somewhat 
> misnamed, because the hostname resolution isn't necessarily done via DNS - in 
> both your example and mine it's done via /etc/hosts.  But the intention is 
> clear.

Agree.

> 
> -- 
> 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/bb366eb5-c7df-452a-9549-281543b1b6d4n%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/DB179F0B-155F-4E09-9646-6F94F9933B15%40gmail.com.


Re: [go-nuts] Trying to understand HTTP connection pooling behaviour - new DNS lookup

2020-11-28 Thread Amit Saha



> On 28 Nov 2020, at 8:14 pm, b.ca...@pobox.com  wrote:
> 
> Do you see a forward or reverse DNS lookup being made?
> 
> Can you provide a standalone program which demonstrates this behaviour?
> 
> I am wondering if either (a) something is logging when the connection is 
> terminated, or (b) a new connection is automatically being made and put into 
> a pool.

Sure, please find my code here: 
https://gist.github.com/amitsaha/3963207e77f7537a5dcc3b3afa8e57ee

My experiment scenario:

1. Modify /etc/hosts to have this entry:

# 216.239.38.21 godoc.org #one of the actual IP addresses
 godoc.org

2. Run godoc locally as: sudo ~/go/bin/godoc -http=:80

3. Build and run the above program:

$ go build -o application
$ ./application http://godoc.org
DNS Start Info: {Host:godoc.org}
DNS Done Info: {Addrs:[{IP:192.168.1.109 Zone:} {IP:2001:4860:4802:38::15 
Zone:} {IP:2001:4860:4802:36::15 Zone:} {IP:2001:4860:4802:32::15 Zone:} 
{IP:2001:4860:4802:34::15 Zone:}] Err: Coalesced:false}
Got Conn: {Conn:0xc000186000 Reused:false WasIdle:false IdleTime:0s}
Put Idle Conn Error: 
Resp protocol: "HTTP/1.1"
Sleeping now

Got Conn: {Conn:0xc000186000 Reused:true WasIdle:true IdleTime:2.00444722s}
Put Idle Conn Error: 
Resp protocol: "HTTP/1.1"
Sleeping now



…

In a new terminal:

1. Update /etc/hosts to have:

216.239.38.21 godoc.org
#192.168.1.109 godoc.org

2. Kill the godoc server

Then in the terminal you had the application running, you should see:

Got Conn: {Conn:0xc000186000 Reused:true WasIdle:true IdleTime:2.001646523s}
Put Idle Conn Error: 
Resp protocol: "HTTP/1.1"
Sleeping now


# This is where the switch to the new IP happens

DNS Start Info: {Host:godoc.org}
DNS Done Info: {Addrs:[{IP:216.239.38.21 Zone:} {IP:2001:4860:4802:32::15 
Zone:} {IP:2001:4860:4802:36::15 Zone:} {IP:2001:4860:4802:38::15 Zone:} 
{IP:2001:4860:4802:34::15 Zone:}] Err: Coalesced:false}
Got Conn: {Conn:0xcb40b8 Reused:false WasIdle:false IdleTime:0s}
Put Idle Conn Error: 
Resp protocol: "HTTP/1.1"
Sleeping now

Got Conn: {Conn:0xcb40b8 Reused:true WasIdle:true IdleTime:2.002040265s}
Put Idle Conn Error: 
Resp protocol: "HTTP/1.1"
Sleeping now

….


My go version:
$ go version
go version go1.15 darwin/amd64


Thank you,
Amit



> 
> On Saturday, 28 November 2020 at 01:27:32 UTC amits...@gmail.com wrote:
> Hi, it seems that the http client automatically performs a DNS lookup when an 
> existing connection is terminated by the server. I simulated it via changing 
> the IP address in /etc/hosts.
> 
> Can someone point me to the code where this logic is checked?
> 
> I am looking at https://golang.org/src/net/http/transport.go but haven’t 
> found something yet - quite a beginner here.
> 
> Thanks,
> Amit.
> 
> 
> -- 
> 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/86f347d0-862f-4922-b2e4-e8835858ae90n%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/8CC6C1E1-BA1C-46C7-A564-E5A9E7D5AA52%40gmail.com.


[go-nuts] Trying to understand HTTP connection pooling behaviour - new DNS lookup

2020-11-27 Thread Amit Saha
Hi, it seems that the http client automatically performs a DNS lookup when an 
existing connection is terminated by the server. I simulated it via changing 
the IP address in /etc/hosts.

Can someone point me to the code where this logic is checked?

I am looking at https://golang.org/src/net/http/transport.go 
 but haven’t found something yet 
- quite a beginner here.

Thanks,
Amit.

-- 
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/748676D6-1E21-4E78-BF3C-87ADC0D8E0E2%40gmail.com.


[go-nuts] Clarification regarding RoundTripper's guidance on Response

2020-11-25 Thread Amit Saha
Hi all, I am wondering whether the RoundTripper’s documentation on what can be 
done with the Response obtained needs further clarification as to is it ok or 
not to modify the response object? 

In addition is it safe to consume the Response from another groutine after/when 
the RoundTripper returns? 

Thanks.

-- 
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/9BD528E2-F7F6-4FE8-9E3B-C52661BF90C0%40gmail.com.


Re: [go-nuts] How to detect if transport connection break

2020-11-20 Thread Amit Saha
On Sat, 21 Nov 2020, 2:12 am Afriyie Abraham Kwabena, <
afriyie.abra...@gmail.com> wrote:

> Hi,
>
> Yes, that is what intend to implement. Can you help with some information
> about how
> I could implement it.
> If possible some libraries or example code.
>

The standard library's http client as defined in net/http uses a transport
layer connection pool already. I believe it will take care of the scenario
you are trying to solve.

This issue https://github.com/golang/go/issues/23427 has some relevant
discussions and starting points for you to explore more. I also recommend
looking at this post https://blog.golang.org/http-tracing for further
research


Abraham
>
> On Friday, November 20, 2020 at 1:07:12 PM UTC+2 Afriyie Abraham Kwabena
> wrote:
>
>> Hi,
>>
>> Am new to programming and if you could explain further what you mean by
>> long lived process.
>>
>>
>> On Friday, November 20, 2020 at 11:41:42 AM UTC+2 amits...@gmail.com
>> wrote:
>>
>>>
>>>
>>> On Fri, 20 Nov 2020, 7:51 pm Afriyie Abraham Kwabena, <
>>> afriyie...@gmail.com> wrote:
>>>

 Hi,

 My basic understanding of HTTP protocol is that an HTTP client sent
 request, get response from the HTTP server and then the connection is
 closed.

 My question is how can an HTTP client detect if the underling transport
 connection break. For example if the HTTP server shutdown, is there a way
 to detect that at the client so that the client can start a persistent
 request.

 If possible, how can I do that in golang HTTP client?

>>>
>>> Are you thinking about implementing a connection pool for your long
>>> lived process?
>>>
>>>
>>>
 BR
 Abraham


 --
 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/4937b529-c1aa-4425-8b81-01214c1fc1f7n%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/d2125284-5529-4c95-82c7-3a8dac7fde78n%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/CANODV3khLBfdEW5HnWv2P6reBNJsxnxL9mBgD%3D6-wkDUBONjdg%40mail.gmail.com.


Re: [go-nuts] How to detect if transport connection break

2020-11-20 Thread Amit Saha
On Fri, 20 Nov 2020, 7:51 pm Afriyie Abraham Kwabena, <
afriyie.abra...@gmail.com> wrote:

>
> Hi,
>
> My basic understanding of HTTP protocol is that an HTTP client sent
> request, get response from the HTTP server and then the connection is
> closed.
>
> My question is how can an HTTP client detect if the underling transport
> connection break. For example if the HTTP server shutdown, is there a way
> to detect that at the client so that the client can start a persistent
> request.
>
> If possible, how can I do that in golang HTTP client?
>

Are you thinking about implementing a connection pool for your long lived
process?



> BR
> Abraham
>
>
> --
> 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/4937b529-c1aa-4425-8b81-01214c1fc1f7n%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/CANODV3nbAtijdg8WybbzpDFcX26-zY-kWNQewV72Qi8VDGq9kw%40mail.gmail.com.


[go-nuts] Re: Testing HTTP client timeout behaviour

2020-11-14 Thread Amit Saha



> On 14 Nov 2020, at 10:28 pm, Amit Saha  wrote:
> 
> Hi all, I am attempting to write a test for http client timeout behaviour. 
> Here’s my bad test server:
> 
> func startBadTestHTTPServer() *httptest.Server {
>   ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r 
> *http.Request) {
>   time.Sleep(60 * time.Second)
>   fmt.Fprint(w, "Hello World")
>   }))
>   return ts
> }
> 
> func TestFetchBadRemoteResource(t *testing.T) {
>   ts := startBadTestHTTPServer()
>   defer ts.Close()
> 
>   client := http.Client{Timeout: 15 * time.Second}
>   data, err := fetchRemoteResource(client, ts.URL)
>   if err != nil {
>   t.Fatal(err)
>   }
> 
>   expected := "Hello World"
>   got := string(data)
> 
>   if expected != got {
>   t.Errorf("Expected response to be: %s, Got: %s", expected, got)
>   }
> }
> 
> When I run the test, I get:
> 
> RUN   TestFetchBadRemoteResource
>fetch_remote_resource_bad_server_test.go:27: Get "http://127.0.0.1:62721": 
> context deadline exceeded (Client.Timeout exceeded while awaiting headers)
> 
> 
> This is expected. But I also get:
> 
> 2020/11/14 22:24:58 httptest.Server blocked in Close after 5 seconds, waiting 
> for connections:
>  *net.TCPConn 0xc000124040 127.0.0.1:62722 in state active
> --- FAIL: TestFetchBadRemoteResource (60.00s)
> 
> 
> I understand why this is happening. But, my test still waits for 60 seconds 
> for the server to shutdown.
> 
> Is there a way around this behaviour where the test server can be forcibly 
> terminated and just carry on?

I didn’t succeed in finding a way for exactly what I wanted. However looking at 
https://golang.org/src/net/http/client_test.go I found a much better way to 
simulate a bad server. Instead a time.Sleep(), have it block on a channel and 
then write to the channel in the test. So my bad server now is:

func startBadTestHTTPServer(shutdownServer chan struct{}) *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r 
*http.Request) {
<-shutdownServer
fmt.Fprint(w, "Hello World")
}))
return ts
}

In the test I then do:

data, err := fetchRemoteResource(client, ts.URL)
if err != nil {
shutdownServer <- struct{}{}
t.Fatal(err)
}



> 
> Thanks,
> Amit
> 
> 

-- 
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/66CC9BF3-EB1F-4ED0-878E-680D74077AA9%40gmail.com.


[go-nuts] Testing HTTP client timeout behaviour

2020-11-14 Thread Amit Saha
Hi all, I am attempting to write a test for http client timeout behaviour. 
Here’s my bad test server:

func startBadTestHTTPServer() *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r 
*http.Request) {
time.Sleep(60 * time.Second)
fmt.Fprint(w, "Hello World")
}))
return ts
}

func TestFetchBadRemoteResource(t *testing.T) {
ts := startBadTestHTTPServer()
defer ts.Close()

client := http.Client{Timeout: 15 * time.Second}
data, err := fetchRemoteResource(client, ts.URL)
if err != nil {
t.Fatal(err)
}

expected := "Hello World"
got := string(data)

if expected != got {
t.Errorf("Expected response to be: %s, Got: %s", expected, got)
}
}

When I run the test, I get:

RUN   TestFetchBadRemoteResource
fetch_remote_resource_bad_server_test.go:27: Get "http://127.0.0.1:62721": 
context deadline exceeded (Client.Timeout exceeded while awaiting headers)


This is expected. But I also get:

2020/11/14 22:24:58 httptest.Server blocked in Close after 5 seconds, waiting 
for connections:
  *net.TCPConn 0xc000124040 127.0.0.1:62722 in state active
--- FAIL: TestFetchBadRemoteResource (60.00s)


I understand why this is happening. But, my test still waits for 60 seconds for 
the server to shutdown.

Is there a way around this behaviour where the test server can be forcibly 
terminated and just carry on?

Thanks,
Amit


-- 
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/94AE404A-6CB1-44EF-AE10-35CC6F87B3D6%40gmail.com.


Re: [go-nuts] Running "go test" modifies go.mod & go.sum

2020-11-01 Thread Amit Saha


> On 1 Nov 2020, at 7:22 pm, 'Dan Kortschak' via golang-nuts 
>  wrote:
> 
> You're using go test, with -mod=readonly, but it's running your code
> with go run, without -mod=readonly.
> 
> Either you'll need to unconditionally pass -mod=readonly to go run in
> the regression_test.go file, or find wether it's been passed to go test
> and then conditionally pass it to go run.
> 
> 
> On Sun, 2020-11-01 at 01:09 -0700, Miki Tebeka wrote:
>> I *do* use "go test", see 
>> https://github.com/tebeka/recheck/blob/master/regression_test.go

I am not sure I don’t see go test in there. But I think Dan already gave you a 
hint.


>> 
>> On Sunday, November 1, 2020 at 8:43:33 AM UTC+2 amits...@gmail.com
>> wrote:
>>> 
>>> On Sun, 1 Nov 2020, 4:07 pm Miki Tebeka, 
>>> wrote:
 Hi,
 
 I wrote a regexp linter (https://github.com/tebeka/recheck)
 that's using golang.org/x/tools/go/analysis.
 
 To test the tool, I run  "go run ./cmd/recheck testdata/ok.go"
 (using os/exec). The problem is that after the test, go.mod &
 go.sum are modified since there are some external imports in the
 go files under testdata.
 
 I've tried using -mod=readonly, building & then running, moving
 the test file to /tmp - all of them didn't work, the mod files
 are still changed after the test.
 
 Any idea how can I prevent the test from modifing the mod files?
>>> 
>>> Your example above suggests you are not using go test to execute
>>> your tests? 
>>> 
 Thanks,
 Miki
> 
> 
> -- 
> 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/80699bdc8a81f300b2b769ae7c8a40dbdead6d7d.camel%40kortschak.io
>  
> .

-- 
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/643937D0-4B19-4008-8999-7D7E63E0F5F3%40gmail.com.


Re: [go-nuts] Running "go test" modifies go.mod & go.sum

2020-11-01 Thread Amit Saha
On Sun, 1 Nov 2020, 4:07 pm Miki Tebeka,  wrote:

> Hi,
>
> I wrote a regexp linter (https://github.com/tebeka/recheck) that's using
> golang.org/x/tools/go/analysis.
>
> To test the tool, I run  "go run ./cmd/recheck testdata/ok.go" (using
> os/exec). The problem is that after the test, go.mod & go.sum are modified
> since there are some external imports in the go files under testdata.
>
> I've tried using -mod=readonly, building & then running, moving the test
> file to /tmp - all of them didn't work, the mod files are still changed
> after the test.
>
> Any idea how can I prevent the test from modifing the mod files?
>

Your example above suggests you are not using go test to execute your
tests?


> Thanks,
> Miki
>
> --
> 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/701453b4-eaa7-4a92-a8a1-520ae6d66fcfn%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/CANODV3my9Dc3t5PkdkSX8RM3PJ65PxByyn5e%2Bj1G-tP%2BGu_Mgg%40mail.gmail.com.


[go-nuts] Context tree inspection/printing

2020-10-22 Thread Amit Saha
Hi all, I have been exploring contexts recently and I am curious does anyone 
know of/have written a tool to analyse packages and print context trees? 

-- 
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/D9DC87AC-E30C-43DB-BA3A-AB19D21F6D16%40gmail.com.


Re: [go-nuts] Table driven tests and error/output testing

2020-10-22 Thread Amit Saha


> On 21 Oct 2020, at 8:31 am, roger peppe  wrote:
> 
> It looks like you're testing a top level command. You might want to consider 
> using the testscript package, which provides a very concise way of end-to-end 
> testing this kind of thing.
> 
> e.g.
> 
> mycommand -h
> cmp stdout expect-stdout
> 
> -- expect-stdout --
> Expected output
> 
> See https://pkg.go.dev/github.com/rogpeppe/go-internal/testscript 
> <https://pkg.go.dev/github.com/rogpeppe/go-internal/testscript> for more 
> details.

This looks interesting, thank you. I will have a look.
> 
> On Tue, 20 Oct 2020 at 05:02, Amit Saha  <mailto:amitsaha...@gmail.com>> wrote:
> Hi all, Consider the following test configuration:
> 
> 
> func TestHandleCommand(t *testing.T) {
> 
> type expectedResult struct {
> output string
> errerror
> }
> type testConfig struct {
> args   []string
> result expectedResult
> }
> 
> testConfigs := []testConfig{
> testConfig{
> args: []string{"-h"},
> result: expectedResult{
> err: nil,
> output: `Expected output`,
> },
> },
> }
> 
> Then, I do this:
> 
> 
> for _, tc := range testConfigs {
> byteBuf := new(bytes.Buffer)
> w := bufio.NewWriter(byteBuf)
> 
> err := handleCommand(w, tc.args)
> if tc.result.err == nil && err != nil {
> t.Errorf("Expected nil error, got %v", err)
> }
> 
> if tc.result.err != nil && err.Error() != tc.result.err.Error() {
> t.Errorf("Expected error %v, got %v", tc.result.err, err)
> }
> 
> if len(tc.result.output) != 0 {
> w.Flush()
> gotOutput := byteBuf.String()
> if tc.result.output != gotOutput {
> t.Errorf("Expected output to be: %v, Got: %v",
> tc.result.output, gotOutput)
> }
> }
> }
> }
> 
> The above pattern works for me since the function may return an error
> and/or it may have something it writes to the provided writer, w.
> 
> Is there a more concise way to write this?
> 
> 
> 
> Thanks,
> Amit.
> 
> -- 
> 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 
> <mailto:golang-nuts%2bunsubscr...@googlegroups.com>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/CANODV3%3DsOA4FKakaQ0mrcC%3D-BnVNq8SdnvDHgXShmrRt-_upHw%40mail.gmail.com
>  
> <https://groups.google.com/d/msgid/golang-nuts/CANODV3%3DsOA4FKakaQ0mrcC%3D-BnVNq8SdnvDHgXShmrRt-_upHw%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/385C8396-3EFB-4895-908A-EC430F1A6186%40gmail.com.


Re: [go-nuts] Table driven tests and error/output testing

2020-10-20 Thread Amit Saha


> On 20 Oct 2020, at 4:13 pm, Tamás Gulácsi  wrote:
> 
> Use bytes.Buffer or strings.Builder directly - no need for the bufio.Writer.
> bufio.Writer only speeds up writing by calling the underlying Writer less, 
> with bigger slices.
> Here you Write into a memory, just as bufio.Writer.
> 
> Also, you can save some allocations by creating the buffer once outside of 
> the loop,
> and Reset() it in each cycle before calling handleCommand.

Thank you, appreciate the suggestions.

> 
> 
> amits...@gmail.com a következőt írta (2020. október 20., kedd, 6:03:31 UTC+2):
> Hi all, Consider the following test configuration: 
> 
> 
> func TestHandleCommand(t *testing.T) { 
> 
> type expectedResult struct { 
> output string 
> err error 
> } 
> type testConfig struct { 
> args []string 
> result expectedResult 
> } 
> 
> testConfigs := []testConfig{ 
> testConfig{ 
> args: []string{"-h"}, 
> result: expectedResult{ 
> err: nil, 
> output: `Expected output`, 
> }, 
> }, 
> } 
> 
> Then, I do this: 
> 
> 
> for _, tc := range testConfigs { 
> byteBuf := new(bytes.Buffer) 
> w := bufio.NewWriter(byteBuf) 
> 
> err := handleCommand(w, tc.args) 
> if tc.result.err == nil && err != nil { 
> t.Errorf("Expected nil error, got %v", err) 
> } 
> 
> if tc.result.err != nil && err.Error() != tc.result.err.Error() { 
> t.Errorf("Expected error %v, got %v", tc.result.err, err) 
> } 
> 
> if len(tc.result.output) != 0 { 
> w.Flush() 
> gotOutput := byteBuf.String() 
> if tc.result.output != gotOutput { 
> t.Errorf("Expected output to be: %v, Got: %v", 
> tc.result.output, gotOutput) 
> } 
> } 
> } 
> } 
> 
> The above pattern works for me since the function may return an error 
> and/or it may have something it writes to the provided writer, w. 
> 
> Is there a more concise way to write this? 
> 
> 
> 
> Thanks, 
> Amit. 
> 
> -- 
> 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/2759fea4-a3d6-42ee-b3e4-2c0ac208f2b9n%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/A3A40B8E-AF4F-43EB-9B75-0AA0464E7E09%40gmail.com.


Re: [go-nuts] Positional arguments and flag package

2020-10-19 Thread Amit Saha
On Thu, Oct 15, 2020 at 4:27 PM Roland Müller  wrote:
>
> Actually, it's explicitly documented, so I don't have to guess:
> https://golang.org/pkg/flag/#hdr-Command_line_flag_syntax

Thank you. I read it and it makes sense why the behavior is like that.

>
>
> Am Do., 15. Okt. 2020 um 08:22 Uhr schrieb Roland Müller :
>>
>> I guess it's usage of flag is meant in to behave like parsing flags in 
>> Unix/LInux, or Python' argparse:
>>
>> after the command you have the flags denoted by '-'
>> after flags follow rest of command line args where elements can be every 
>> character string
>>
>> BR,
>> Roland
>>
>> Am Fr., 9. Okt. 2020 um 07:10 Uhr schrieb Amit Saha :
>>>
>>> Hi all, I realize that the flag package stops parsing os.Args[] once it 
>>> finds a non "-" character. This means, if I invoke my program as:
>>>
>>> $ ./myprog arg1 -d value
>>>
>>> flag.Parse() will stop parsing the moment it sees arg1 and result in NArg() 
>>> returning 2 instead of 1.
>>>
>>> Is there a recommended workaround to handle both $./myprog -d value arg1 
>>> and $./myprog arg1 -d value correctly?
>>>
>>> Thanks for any suggestions.
>>>
>>> Thanks,
>>> Amit.
>>>
>>> --
>>> 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/d1399079-4589-454f-91d3-820a60aa239dn%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/CANODV3nDeOQK%3DOkPMXtMuSQpkKkvLLG7NjO%3D9AJ60_QMvjc%3DfQ%40mail.gmail.com.


  1   2   >