[go-nuts] why rot13Reader example run twice?
Hello, why the method Read of rot13Reader run twice? This is the code: package main import ( "fmt" "io" "os" "strings" ) type rot13Reader struct { r io.Reader } var k int func (rot *rot13Reader) Read(p []byte) (n int, err error) { k++ n, err = rot.r.Read(p) for i := 0; i < len(p); i++ { if (p[i] >= 'A' && p[i] < 'N') || (p[i] >= 'a' && p[i] < 'n') { p[i] += 13 } else if (p[i] > 'M' && p[i] <= 'Z') || (p[i] > 'm' && p[i] <= 'z') { p[i] -= 13 } } return } func main() { s := strings.NewReader("Lbh penpxrq gur pbqr!") r := rot13Reader{s} io.Copy(os.Stdout, &r) fmt.Println("\n", k) } Why after the execution the k value is 2? T.i.a Franco -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Re: why rot13Reader example run twice?
Sorry but the method work twice. try this: } func (rot *rot13Reader) Read(p []byte) (n int, err error) { k++ n, err = rot.r.Read(p) for i := 0; i < len(p); i++ { if (p[i] >= 'A' && p[i] < 'N') || (p[i] >= 'a' && p[i] < 'n') { p[i] += 13 } else if (p[i] > 'M' && p[i] <= 'Z') || (p[i] > 'm' && p[i] <= 'z') { p[i] -= 13 } } fmt.Println("\n", k, string(p)) return } Il giorno venerdì 30 giugno 2017 10:24:22 UTC+2, Dave Cheney ha scritto: > > Oh, sorry, I know. It'll be because the first time through your reader > returns , nil, and on the second time returns 0, io.EOF. So Read is called > twice, but on the second time does no work. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Re: why rot13Reader example run twice?
Anyway thank you for the answer. I know whats the problem. This is an example of "cargo cult programming" This is where I read the solutions. https://gist.github.com/zyxar/2317744 -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] why rot13Reader example run twice?
1. Because with 2 there is a problem. The method is called twice and is inefficient. Il giorno venerdì 30 giugno 2017 11:31:31 UTC+2, Jan Mercl ha scritto: > > On Fri, Jun 30, 2017 at 9:48 AM Franco Marchesini > wrote: > > > Why after the execution the k value is 2? > > What value of k do you expect instead and why? ( > https://play.golang.org/p/wdWo3fahAS) > > -- > > -j > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] why rot13Reader example run twice?
Yes, after Dave's answer I realized the mistake. This is a solution. Il giorno venerdì 30 giugno 2017 11:42:45 UTC+2, Sebastien Binet ha scritto: > > > > On Fri, Jun 30, 2017 at 11:38 AM, Franco Marchesini > wrote: > >> 1. >> >> Because with 2 there is a problem. The method is called twice and is >> inefficient. >> > > what about checking your errors, then ? :) > https://play.golang.org/p/K8j8D0AZe9 > > -s > > >> Il giorno venerdì 30 giugno 2017 11:31:31 UTC+2, Jan Mercl ha scritto: >>> >>> On Fri, Jun 30, 2017 at 9:48 AM Franco Marchesini >>> wrote: >>> >>> > Why after the execution the k value is 2? >>> >>> What value of k do you expect instead and why? ( >>> https://play.golang.org/p/wdWo3fahAS) >>> >>> -- >>> >>> -j >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to golang-nuts...@googlegroups.com . >> For more options, visit https://groups.google.com/d/optout. >> > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] why rot13Reader example run twice?
Aside from the correction, however, I did not understand why the method is called 2 times. Il giorno venerdì 30 giugno 2017 12:51:20 UTC+2, Franco Marchesini ha scritto: > > Yes, after Dave's answer I realized the mistake. > This is a solution. > > Il giorno venerdì 30 giugno 2017 11:42:45 UTC+2, Sebastien Binet ha > scritto: >> >> >> >> On Fri, Jun 30, 2017 at 11:38 AM, Franco Marchesini < >> franco.m...@gmail.com> wrote: >> >>> 1. >>> >>> Because with 2 there is a problem. The method is called twice and is >>> inefficient. >>> >> >> what about checking your errors, then ? :) >> https://play.golang.org/p/K8j8D0AZe9 >> >> -s >> >> >>> Il giorno venerdì 30 giugno 2017 11:31:31 UTC+2, Jan Mercl ha scritto: >>>> >>>> On Fri, Jun 30, 2017 at 9:48 AM Franco Marchesini < >>>> franco.m...@gmail.com> wrote: >>>> >>>> > Why after the execution the k value is 2? >>>> >>>> What value of k do you expect instead and why? ( >>>> https://play.golang.org/p/wdWo3fahAS) >>>> >>>> -- >>>> >>>> -j >>>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "golang-nuts" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to golang-nuts...@googlegroups.com. >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] why rot13Reader example run twice?
Now that I looked at the source, I learned. // Reader is the interface that wraps the basic Read method. // // Read reads up to len(p) bytes into p. It returns the number of bytes // read (0 <= n <= len(p)) and any error encountered. Even if Read // returns n < len(p), it may use all of p as scratch space during the call. // If some data is available but not len(p) bytes, Read conventionally // returns what is available instead of waiting for more. // // When Read encounters an error or end-of-file condition after // successfully reading n > 0 bytes, it returns the number of // bytes read. It may return the (non-nil) error from the same call // or return the error (and n == 0) from a subsequent call. // An instance of this general case is that a Reader returning // a non-zero number of bytes at the end of the input stream may // return either err == EOF or err == nil. The next Read should // return 0, EOF. // // Callers should always process the n > 0 bytes returned before // considering the error err. Doing so correctly handles I/O errors // that happen after reading some bytes and also both of the // allowed EOF behaviors. // // Implementations of Read are discouraged from returning a // zero byte count with a nil error, except when len(p) == 0. // Callers should treat a return of 0 and nil as indicating that // nothing happened; in particular it does not indicate EOF. Sorry for bother. Thank you all. Il giorno venerdì 30 giugno 2017 15:01:55 UTC+2, Jan Mercl ha scritto: > > > On Fri, Jun 30, 2017 at 2:42 PM Franco Marchesini > wrote: > > > Aside from the correction, however, I did not understand why the method > is called 2 times. > > HTH: https://play.golang.org/p/Na7GtgZ-_B > > > -- > > -j > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Re: max connections limit lib/pq
If you really have over 100 simultaneous connection you need to configure postgresql engine Regards Franco Il giorno mercoledì 21 giugno 2017 14:56:14 UTC+2, Tieson Molly ha scritto: > > I am running into an issue I have seen mentioned on here years ago. I am > seeking the current best solution. > > I have a web server connecting to a postgresql database using the standard > database/sql and the lib/pq driver. I keep running out of connections over > time as it seems the pool grows up to 100. > > Is there a way to prevent or fix this? > > Best regards, > > Ty > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Re: max connections limit lib/pq
Postgres default max connection is set to 100. https://www.postgresql.org/docs/9.5/static/runtime-config-connection.html max_connections (integer) Determines the maximum number of concurrent connections to the database server. The default is typically 100 connections, but might be less if your kernel settings will not support it (as determined during initdb). This parameter can only be set at server start. When running a standby server, you must set this parameter to the same or higher value than on the master server. Otherwise, queries will not be allowed in the standby server. Il giorno venerdì 30 giugno 2017 20:40:36 UTC+2, Gerald Stan ha scritto: > > what you want is to set SetMaxOpenConns, SetMaxIdleConns: > > db, err := sql.Open(config.DriverName, GetDataSourceFromConfig(config)) > if err != nil { > log.Fatal(err) > } > > db.SetMaxOpenConns(config.MaxOpenConns) > db.SetMaxIdleConns(config.MaxIdleConns) > > > On Friday, June 30, 2017 at 10:22:11 AM UTC-4, Franco Marchesini wrote: >> >> If you really have over 100 simultaneous connection you need to configure >> postgresql engine >> Regards >> Franco >> >> Il giorno mercoledì 21 giugno 2017 14:56:14 UTC+2, Tieson Molly ha >> scritto: >>> >>> I am running into an issue I have seen mentioned on here years ago. I >>> am seeking the current best solution. >>> >>> I have a web server connecting to a postgresql database using the >>> standard database/sql and the lib/pq driver. I keep running out of >>> connections over time as it seems the pool grows up to 100. >>> >>> Is there a way to prevent or fix this? >>> >>> Best regards, >>> >>> Ty >>> >>> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] How I can translate this shell command to golang code?
Help, how I can translate this shell command : echo -n 123456 | dmtxwrite -s 16x48 -o image.png to golang exec.Commad() Thanks in advance Franco -- 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/c6aaba94-453b-41dc-83a4-ffd4f3912494o%40googlegroups.com.
Re: [go-nuts] How I can translate this shell command to golang code?
Ciao, I had read this tutorial, but my mistake was not having called the shell command Il giorno lunedì 22 giugno 2020 18:01:04 UTC+2, Michael Jones ha scritto: > > https://tutorialedge.net/golang/executing-system-commands-with-golang/ > > On Mon, Jun 22, 2020 at 8:54 AM Franco Marchesini > wrote: > >> Help, >> >> how I can translate this shell command : >> >> echo -n 123456 | dmtxwrite -s 16x48 -o image.png >> >> to golang >> >> exec.Commad() >> >> Thanks in advance >> Franco >> >> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to golan...@googlegroups.com . >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/c6aaba94-453b-41dc-83a4-ffd4f3912494o%40googlegroups.com >> >> <https://groups.google.com/d/msgid/golang-nuts/c6aaba94-453b-41dc-83a4-ffd4f3912494o%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > > > -- > > *Michael T. jonesmichae...@gmail.com * > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/e84a97ec-d326-4ccc-8e31-bf25991ec897o%40googlegroups.com.
Re: [go-nuts] How I can translate this shell command to golang code?
Yes, this is better. Thanks func main() { cmd := exec.Command("dmtxwrite", "-s", "16x48", "-o", "image2.png") cmd.Stdin = strings.NewReader("123456") if err := cmd.Run(); err != nil { panic(err.Error()) } } Il giorno lunedì 22 giugno 2020 18:19:36 UTC+2, Bakul Shah ha scritto: > > You can avoid calling the shell by observing that you are simply passing > "123456" as input, which you can do in a Go Program itself. > See https://golang.org/pkg/os/exec/#example_Command > > > On Jun 22, 2020, at 8:54 AM, Franco Marchesini > wrote: > > Help, > > how I can translate this shell command : > > echo -n 123456 | dmtxwrite -s 16x48 -o image.png > > to golang > > exec.Commad() > > Thanks in advance > Franco > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golan...@googlegroups.com . > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/c6aaba94-453b-41dc-83a4-ffd4f3912494o%40googlegroups.com > > <https://groups.google.com/d/msgid/golang-nuts/c6aaba94-453b-41dc-83a4-ffd4f3912494o%40googlegroups.com?utm_medium=email&utm_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/2578ddd7-5d86-44f2-80e2-16872553a03ao%40googlegroups.com.