[go-nuts] Re: Underlying arrays and their slices passed to functions

2018-01-05 Thread jobs jobs
这个问题有意思。

package main

import (
"fmt"
)

func main() {

out := []byte{1, 2, 3, 4}
fmt.Println(out)

proc(&out)
fmt.Println(out)

proc2(out)
fmt.Println(out)

}

func proc(p *[]byte) {
*p = []byte{5,6,7,8}
}

func proc2(p []byte) {
p[0] = 5
p[1] = 6
p[2] = 7
p[3] = 8
}

like this can get what you want.

the problem is value copied or  memory addres copied


the output is:

[1 2 3 4]
[5 6 7 8]
[5 6 7 8]



在 2018年1月5日星期五 UTC+8上午2:08:04,Frank Davidson写道:
>
> I'm sure this has probably been answered before, but I have a question 
> about when a slice's underlying array is copied? In this code:
>
> https://play.golang.org/p/TnMFo-rYKzq
>
> package main
>
> import (
> "fmt"
> )
>
> func main() {
>
> out := []byte{1, 2, 3, 4}
> fmt.Println(out)
> proc(out)
> fmt.Println(out)
> proc2(out)
> fmt.Println(out) 
>
> }
>
> func proc(p []byte) {
> p = []byte{5,6,7,8}
> }
>
> func proc2(p []byte) {
> p[0] = 5
> p[1] = 6
> p[2] = 7
> p[3] = 8
> }
>
>
> The output is:
>
> [1 2 3 4]
> [1 2 3 4] 
>
> [5 6 7 8] 
>
>
> I assume that in proc the slice's underlying array is being copied as 
> well? Hence p is referring to a new array within the function and, thus, 
> the underlying original array is not modified, even though it's capacity 
> wouldn't have to change? But in proc2 there is no copy of the underlying 
> array made, hence the original array is modified? What is the rule for when 
> the underlying array is also copied and when it is not?
>

-- 
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: How to send multiple responses (file upload progress) to the client?

2018-01-05 Thread Ain
On Friday, January 5, 2018 at 10:51:09 PM UTC+2, Jason Lee wrote:
>
> I've set up a GoLang post request handler that uploads a file to Google 
> Cloud Storage.
>
> Now I'd like to figure out how I can send the upload progress info back to 
> the client before the whole thing finishes.
>

If the client is browser and uses XMLHttpRequest to POST the file then it 
can use XMLHttpRequest.upload.onprogress eventhandler to track the 
progress. No need to do anything on the server side for that to work.

 
HTH
ain

-- 
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] Very simple, Td array

2018-01-05 Thread Tong Sun
On Fri, Jan 5, 2018 at 7:53 PM, Tong Sun  wrote:

> That's OK Dave. It's over.
>
> All that I wanted is to confirm is that the usage of `tr.Td[1])` was
> right. Had I had that faith, there wouldn't have been this email, which
> apparently I was focusing on `tr.Td[1])` instead of what I should
> be focusing.
>
> I.e., the answer I was expecting was just merely, *the usage of
> `tr.Td[1])` was fine, the problem is else where*. Nothing more, but
> thanks for helping.
>

Just to answer your request, yes, I did prepare a small complete,
stand-alone, example, from which I found out that the usage of `tr.Td[1])`
was fine, then started to look else where.

On Fri, Jan 5, 2018 at 7:17 PM, Dave Cheney  wrote:
>
>> The best way to get help for this is to show us precisely what you
>> did, ideally in a small complete, stand-alone, example, and tell us
>> what you expected to happen, and tell us precisely what happened
>> instead.
>>
>> --
>> 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/to
>> pic/golang-nuts/heZfZ-OZOSc/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> golang-nuts+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Very simple, Td array

2018-01-05 Thread Tong Sun
That's OK Dave. It's over.

All that I wanted is to confirm is that the usage of `tr.Td[1])` was right.
Had I had that faith, there wouldn't have been this email, which apparently
I was focusing on `tr.Td[1])` instead of what I should be focusing.

I.e., the answer I was expecting was just merely, *the usage of `tr.Td[1])`
was fine, the problem is else where*. Nothing more, but thanks for helping.



On Fri, Jan 5, 2018 at 7:17 PM, Dave Cheney  wrote:

> The best way to get help for this is to show us precisely what you
> did, ideally in a small complete, stand-alone, example, and tell us
> what you expected to happen, and tell us precisely what happened
> instead.
>
> --
> 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/heZfZ-OZOSc/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Go Community Charity Work?

2018-01-05 Thread matthewjuran
From 
https://www.pharmamanufacturing.com/experts/contract-manufacturing-management-supply-chain-management-/show/71/

In the US: Food and Drug Administration Safety and Innovation Act (FDASIA), 
Drug Supply Chain Security Act (DSCSA)

Don mentions the term "end-to-end security program".

Here's a paper on pharmaceutical product tracking and 
authentication: 
ftp://ftp.hp.com/pub/services/manufacturing/info/pta_59830764en.pdf

A paper from 
FedEx: 
http://images.fedex.com/us/supply-chain/services/healthcare-shared-network/HSN-End-to-End-security.pdf

An article on the state of these activities in 
2017: 
http://pharmaceuticalcommerce.com/supply-chain-logistics/2017-product-security-report/

I'll read these in detail and report any significant information.

Matt

On Friday, December 29, 2017 at 4:27:52 PM UTC-6, KLR wrote:
>
>
> EU has also been at it 
> https://ec.europa.eu/health/sites/health/files/files/counterf_par_trade/doc_publ_consult_200803/114_b_efpia_en.pdf
>
> El domingo, 24 de diciembre de 2017, 17:38:57 (UTC+1), Frank Davidson 
> escribió:
>>
>> A few weeks ago, I saw this report on the PBS NewsHour:
>>
>>
>> https://www.pbs.org/newshour/show/fighting-the-public-health-threat-of-counterfeit-drugs
>>  
>> 
>>
>> I have just (as in haven't even started putting code in GitHub yet) 
>> started to work on an app which I was thinking might help deal with this 
>> and truly help folks out, worldwide. It also could help with drugs bought 
>> over the internet as well, I would think.
>>
>> The idea is to use aztec bar codes to encode a manufacturer's ECDSA 
>> signature and a UUID and possible some other metadata on the label of a 
>> bottle of pharmaceuticals.
>>
>> An Android and iOS app would then allow an individual or small pharmacy 
>> to scan that bottle, verifying the signature against a manufacturer's 
>> website, where there would be verification against their public key and 
>> also their cert with their cert authority. Smartphones and the Internet are 
>> fast becoming ubiquitous even in the developing world.
>>
>> I'm working on the code right now and I'll post things as I progress.
>>
>> I'm not looking for any money at all because I think the programming task 
>> isn't really too hard, but, I was thinking that the greater Go community 
>> might also have an interest.
>>
>> I'm not sure if there's a charitable team or group or anything, but I 
>> thought I'd check it out. I'd like to make this totally OSS with a 
>> non-profit backing it (if it gets to that state eventually). I've 
>> registered verx.codes as a domain.
>>
>> I'm not totally sure yet if this is feasible in the real world or if the 
>> pharmaceutical companies could be persuaded to buy in, but I would think it 
>> could also be good for their brands and give them a way to track individual 
>> bottles and provide authenticated info to users like expiration dates and 
>> extensive use instructions once a person successfully scans and verifies a 
>> code and then gets passed to the company's website.
>>
>> Anyway, totally just beginning and I'd welcome any thoughts, comments, 
>> and/or critisms!
>>
>> Merry Christmas and Happy Holidays to all the Go community!
>>
>> Frank
>>
>

-- 
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] Very simple, Td array

2018-01-05 Thread Dave Cheney
The best way to get help for this is to show us precisely what you 
did, ideally in a small complete, stand-alone, example, and tell us 
what you expected to happen, and tell us precisely what happened 
instead. 

-- 
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: ListenAndServ and channels

2018-01-05 Thread matthewjuran
Using a channel read in the http handler will block the ticker for loop 
until the read occurs, but if you want that then pass mc into the generic 
function: https://play.golang.org/p/GrHQxPhy1qu

Using a buffered channel will keep the history of ticks and not block but 
may run out of buffer space if the handler isn't called enough.

Amnon's global var should work if you are just looking for the current tick 
value.

Matt

On Friday, January 5, 2018 at 12:33:32 PM UTC-6, Amnon Baron Cohen wrote:
>
> try using a global var.
> something like https://play.golang.org/p/05-xBDh5rgn
>
> On Thursday, 4 January 2018 15:09:41 UTC, Keith Brown wrote:
>>
>> I am trying to Serve a webpage while running a ticker in the background. 
>> I am trying to generate a random number, genRandom() , periodically and 
>> publish it on a channel so generic() can see the results. I have something 
>> like this https://play.golang.org/p/6d1xmqpUYY7 but I don't know how to 
>> get channel results to generic()? Do I need to make my mc channel a global? 
>>
>

-- 
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: How to send multiple responses (file upload progress) to the client?

2018-01-05 Thread matthewjuran
Can you have the client make concurrent periodic get requests that return 
the current status?

Alternatively websockets (https://github.com/gorilla/websocket) may work 
depending on your deployment needs (Google App Engine doesn’t support them 
yet and old web browsers don’t support them).

Matt

On Friday, January 5, 2018 at 2:51:09 PM UTC-6, Jason Lee wrote:
>
> I've set up a GoLang post request handler that uploads a file to Google 
> Cloud Storage.
>
> Now I'd like to figure out how I can send the upload progress info back to 
> the client before the whole thing finishes.
>
> Through lots of searching, I've created a custom PassThru struct that 
> prints the progress in console. But I can't figure out how to send the 
> progress updates periodically back to the client.
>
> When I do fmt.Fprint(w, string), it waits for the whole thing to finish 
> and the client receives all of the written data at once.
>
> Can anyone please provide some help?
>

-- 
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 to send multiple responses (file upload progress) to the client?

2018-01-05 Thread jason . saeho . lee
I've set up a GoLang post request handler that uploads a file to Google 
Cloud Storage.

Now I'd like to figure out how I can send the upload progress info back to 
the client before the whole thing finishes.

Through lots of searching, I've created a custom PassThru struct that 
prints the progress in console. But I can't figure out how to send the 
progress updates periodically back to the client.

When I do fmt.Fprint(w, string), it waits for the whole thing to finish and 
the client receives all of the written data at once.

Can anyone please provide some help?

-- 
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: ListenAndServ and channels

2018-01-05 Thread Amnon Baron Cohen
try using a global var.
something like https://play.golang.org/p/05-xBDh5rgn

On Thursday, 4 January 2018 15:09:41 UTC, Keith Brown wrote:
>
> I am trying to Serve a webpage while running a ticker in the background. I 
> am trying to generate a random number, genRandom() , periodically and 
> publish it on a channel so generic() can see the results. I have something 
> like this https://play.golang.org/p/6d1xmqpUYY7 but I don't know how to 
> get channel results to generic()? Do I need to make my mc channel a global? 
>

-- 
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] Meet "fatal morestack on g0" on Linux

2018-01-05 Thread Ian Lance Taylor
On Fri, Jan 5, 2018 at 7:17 AM,   wrote:
>
> I meet a strange problem when running a program on Linux. I get "fatal:
> morestack on g0" from stderr. The process is still there but does not
> respond anymore. When I use `curl
> http://ip:port/debug/pprof/goroutine?debug=1` to check the stack, but it
> halts. There is nothing useful in stderr or dmesg.

(I would like to encourage you and everyone else to not post
screenshots of text.  Just include the text in the e-mail message as
text.  Screenshots of images, sure, but not text.  Thanks.)

The error "fatal: morestack on g0" should, of course, never happen.
The first questions are standard: have you run your program with the
race detector?  Do you use cgo or the unsafe package?

Beyond that, does the problem happen consistently?  Is there a way
that we can reproduce it?

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.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Using database/sql in a web application

2018-01-05 Thread Chris Hopkins
Why not use an interface?
type TableSaver func (db *DB)
or 
type TableSaver func (tx *Tx) // depending on your needs


Allowing you to:
func (t *Table) SaveTable (...) {}


Which in your example if you wanted use a different SaveTable  
implementation for OtherTable you could. Otherwise it would just inherit it 
from Table.
Or am I missing something here?

Chris

On Friday, 5 January 2018 16:11:53 UTC, Manlio Perillo wrote:
>
> Il giorno venerdì 5 gennaio 2018 16:57:04 UTC+1, Ayan George ha scritto:
>>
>>
>>
>> On 01/05/2018 10:16 AM, Manlio Perillo wrote: 
>> > Recently I have developed a few web applications in Go, using a 
>> > PostgreSQL database, but I have yet understand the best way to use 
>> > the database/sql package. 
>> > 
>>
>> I don't know how you're serving web pages but I typically use net/http. 
>> This also means that I typically build an http.Server.  This requires 
>> that I create a type that conforms to the http.Handler interface. 
>>
>
> Yes, I use the standard net/http interface, without frameworks (but I use 
> a simple toolkit written by me).
>
>>
>> You can use this type to pass around all of the data you need, including 
>> sql.DB instances, log.Logger, etc. 
>>
>> So here's the skeleton of what I normally do.  I haven't tested this -- 
>> this is just to get the point across.  Finally, this may be completely 
>> off base.  I'm open to any criticisms etc. anyone may have. 
>>
>> package main 
>>
>> import ( 
>>   "database/sql" 
>>   _ "github.com/lib/pq" 
>>   "log" 
>>   "net/http" 
>>   "time" 
>> ) 
>>
>> type app struct { 
>>   db *sql.DB 
>> } 
>>
>> func (a *app) ServeHTTP(w http.ResponseWriter, r *http.Request) { 
>>   // here we can use all of the fields of our app struct -- including 
>> the db! 
>>
>>   row := db.QueryRow("select current_timestamp;") 
>>
>>   var time string 
>>   err := row.Scan(&time) 
>>
>>   if err != nil { 
>> // handle the error.  maybe log it?  i typically 
>> // create a log.Logger instance in my app and use it 
>> // at times like these. 
>> return 
>>   } 
>>
>>   w.Write([]byte("hello, world!")) 
>> } 
>>
>> func main() { 
>>   db, err := sql.Open("postgres", "...") 
>>
>>   if err != nil { 
>> log.Fatal(err) 
>>   } 
>>
>>   if err := db.Ping(); err != nil { 
>> log.Fatal(err) 
>>   } 
>>
>>   myapp := app{ 
>> db: db, 
>>   } 
>>
>>   srv := http.Server{ 
>> Addr: ":8080", 
>> ReadTimeout:  1 * time.Second, 
>> WriteTimeout: 1 * time.Second, 
>> Handler:  &myapp, 
>>   } 
>>
>>   log.Fatal(srv.ListenAndServe()) 
>> } 
>>
>
> Thanks, this is  a very good solution.
>
> Currently I'm using something different, however.
> I have a package that exports the same API as database/sql, but as free 
> functions using a global *DB configured when loading a configuration file.
>
> However the question is the same, and it is how and what to pass to 
> functions that need a connection to the database.
>
> Passing *DB is no good; take as an example a "generic" package having
>
>   type Table struct {
>   A int
>   B string
>   }
>
>   func SaveTable(t *Table, db *DB) ...
>
> or
>   func SaveTable(t *Table, tx *Tx) ...
>
> The function does not need to know if it is inside a transaction; it is 
> the responsibility of the caller.
>
> However consider a package with
>
>   type Table struct {
>   A int
>   B string
>   }
>
>   type OtherTable struct {
>   Table
>   C float64
>   B bool
>   }
>
>
> Now the function SaveOtherTable may want to use a transaction to save 
> Table and OtherTable separately.
> You surely can not pass a *Tx, since it does not support nested 
> transactions.
>
>
>
> Thanks
> Manlio Perillo
>
>
>

-- 
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] Using database/sql in a web application

2018-01-05 Thread Manlio Perillo
Il giorno venerdì 5 gennaio 2018 16:57:04 UTC+1, Ayan George ha scritto:
>
>
>
> On 01/05/2018 10:16 AM, Manlio Perillo wrote: 
> > Recently I have developed a few web applications in Go, using a 
> > PostgreSQL database, but I have yet understand the best way to use 
> > the database/sql package. 
> > 
>
> I don't know how you're serving web pages but I typically use net/http. 
> This also means that I typically build an http.Server.  This requires 
> that I create a type that conforms to the http.Handler interface. 
>

Yes, I use the standard net/http interface, without frameworks (but I use a 
simple toolkit written by me).

>
> You can use this type to pass around all of the data you need, including 
> sql.DB instances, log.Logger, etc. 
>
> So here's the skeleton of what I normally do.  I haven't tested this -- 
> this is just to get the point across.  Finally, this may be completely 
> off base.  I'm open to any criticisms etc. anyone may have. 
>
> package main 
>
> import ( 
>   "database/sql" 
>   _ "github.com/lib/pq" 
>   "log" 
>   "net/http" 
>   "time" 
> ) 
>
> type app struct { 
>   db *sql.DB 
> } 
>
> func (a *app) ServeHTTP(w http.ResponseWriter, r *http.Request) { 
>   // here we can use all of the fields of our app struct -- including 
> the db! 
>
>   row := db.QueryRow("select current_timestamp;") 
>
>   var time string 
>   err := row.Scan(&time) 
>
>   if err != nil { 
> // handle the error.  maybe log it?  i typically 
> // create a log.Logger instance in my app and use it 
> // at times like these. 
> return 
>   } 
>
>   w.Write([]byte("hello, world!")) 
> } 
>
> func main() { 
>   db, err := sql.Open("postgres", "...") 
>
>   if err != nil { 
> log.Fatal(err) 
>   } 
>
>   if err := db.Ping(); err != nil { 
> log.Fatal(err) 
>   } 
>
>   myapp := app{ 
> db: db, 
>   } 
>
>   srv := http.Server{ 
> Addr: ":8080", 
> ReadTimeout:  1 * time.Second, 
> WriteTimeout: 1 * time.Second, 
> Handler:  &myapp, 
>   } 
>
>   log.Fatal(srv.ListenAndServe()) 
> } 
>

Thanks, this is  a very good solution.

Currently I'm using something different, however.
I have a package that exports the same API as database/sql, but as free 
functions using a global *DB configured when loading a configuration file.

However the question is the same, and it is how and what to pass to 
functions that need a connection to the database.

Passing *DB is no good; take as an example a "generic" package having

  type Table struct {
  A int
  B string
  }

  func SaveTable(t *Table, db *DB) ...

or
  func SaveTable(t *Table, tx *Tx) ...

The function does not need to know if it is inside a transaction; it is the 
responsibility of the caller.

However consider a package with

  type Table struct {
  A int
  B string
  }

  type OtherTable struct {
  Table
  C float64
  B bool
  }


Now the function SaveOtherTable may want to use a transaction to save Table 
and OtherTable separately.
You surely can not pass a *Tx, since it does not support nested 
transactions.



Thanks
Manlio Perillo


-- 
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: Using database/sql in a web application

2018-01-05 Thread matthewjuran
Here’s an overview of what I’ve done, but no quality claim is made.

In package main there’s a global:

type DB struct {
*sql.DB
}

var database DB

In func main() I call an initialization function that reads a JSON 
configuration file and does sql.Open with dbname, user, password, host, 
port, and sslmode from the file, result assigned to database.DB. There’s a 
database.Ping() then database.SetMaxIdleConns and database.SetMaxOpenConns. 
Any error causes a panic.

The rest of package main has many methods on DB. An example is all of my 
handlers call database.validSession(r). The method approach is so that I 
can configure a test database when writing unit tests for package main as 
suggested by Google search results awhile back. In some cases database 
corruption can happen in my app by concurrent DB.Query calls followed by 
DB.Exec. I don’t change the postgres transaction isolation, but some 
methods are on TX instead of DB because of these race conditions.

type TX struct {
*sql.Tx
}

// error handling for sql.DB.Begin and sql.Tx.Commit is always a panic
func (db DB) Begin() TX {

func (tx TX) Commit() {

One of my TX methods has a forWrite bool to add a FOR UPDATE to the query 
because SELECT won’t lock the row for a transaction without it in postgres 
at the default transaction isolation level.

Here’s an example query:

err = db.QueryRow("SELECT "+session_name+" FROM "+session_table+" WHERE "+
session_key+"=$1;", keyCookie.Value).Scan(&name)

I have an open issue for figuring out how to make this better/cleaner, but 
at least all of my queries rely on consts for key names. Error handling for 
database interaction is quite variable depending on the use of the 
surrounding function. Any unhandleable error results in a panic, and 
sql.ErrNoRows is useful.

I haven’t used the context package, or a middleware pattern besides 
copy-paste of function calls in handlers.

Some of my code is messy with these patterns. Commit can’t always be 
deferred which brings us back to writing it in at every return point. The 
choices of where transactions are opened in relation to the method uses 
seems arbitrary because of layering fixes over time without a rewrite. I 
haven’t written package main unit tests yet so I can’t comment on how much 
value methods on DB and TX add. But problems are mostly straightforward to 
fix for now.

My current function structure causes transactions to overlap within one 
path of execution sometimes (a TX is open during a DB query or exec), but 
this seems to not cause problems since they are guaranteed to be accessing 
different tables in my case.

Here’s the app source, all database interaction is in package main: 
https://github.com/pciet/wichess

Concurrent request load testing helped me find 5+ problems with my database 
interactions. Transactions aren't completely straightforward to get right.

Matt

On Friday, January 5, 2018 at 9:16:34 AM UTC-6, Manlio Perillo wrote:
>
> Recently I have developed a few web applications in Go, using a PostgreSQL 
> database, but I have yet understand the best way to use the database/sql 
> package.
>
> The simplest method seems to start a transaction in the HTTP handler and 
> pass a *sql.Tx value to all the functions that need to access the database.
> However I'm afraid that this method is "too" simple.
>
> One possible problem is an HTTP handler acting as a middleware, and both 
> the "middleware" an the "normal" HTTP handler needs a transaction.
> Using the "simplest method", the middleware and the handler will end up 
> using two distinct transaction, and this may not be what one expects.
>
>
> The other solution is to do what frameworks like Django do, storing 
> something like
>
>   type Interface interface {
>   func Exec(query string, ...) ...
>   func Query(query string, ...) ...
>   func InTransaction) bool
>   }
>
> as a per request data, including transaction state, (ab)using 
> Context.WithValue.  But I suspect that this is not a good idea.
>
>
> Thanks  Manlio
>

-- 
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] Underlying arrays and their slices passed to functions

2018-01-05 Thread Frank Davidson
Thanks, all!!!

Frank

On Friday, January 5, 2018 at 4:25:33 AM UTC-5, Ian Davis wrote:
>
> On Thu, 4 Jan 2018, at 6:55 PM, Frank Davidson wrote:
>
> Thanks!!! Very helpful blog post!!
>
> So, in proc, the slice header is copied, then an entirely new array is 
> created - []byte{5,6,7,8} - and the slice header copy is set to point at 
> that new array, and then discarded, whereas in proc 2, the slice header is 
> not reset, and so still points to the original array?
>
>
> Yes
>

-- 
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] Using database/sql in a web application

2018-01-05 Thread Ayan George


On 01/05/2018 10:16 AM, Manlio Perillo wrote:
> Recently I have developed a few web applications in Go, using a 
> PostgreSQL database, but I have yet understand the best way to use
> the database/sql package.
> 

I don't know how you're serving web pages but I typically use net/http.
This also means that I typically build an http.Server.  This requires
that I create a type that conforms to the http.Handler interface.

You can use this type to pass around all of the data you need, including
sql.DB instances, log.Logger, etc.

So here's the skeleton of what I normally do.  I haven't tested this --
this is just to get the point across.  Finally, this may be completely
off base.  I'm open to any criticisms etc. anyone may have.

package main

import (
  "database/sql"
  _ "github.com/lib/pq"
  "log"
  "net/http"
  "time"
)

type app struct {
  db *sql.DB
}

func (a *app) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  // here we can use all of the fields of our app struct -- including
the db!

  row := db.QueryRow("select current_timestamp;")

  var time string
  err := row.Scan(&time)

  if err != nil {
// handle the error.  maybe log it?  i typically
// create a log.Logger instance in my app and use it
// at times like these.
return
  }

  w.Write([]byte("hello, world!"))
}

func main() {
  db, err := sql.Open("postgres", "...")

  if err != nil {
log.Fatal(err)
  }

  if err := db.Ping(); err != nil {
log.Fatal(err)
  }

  myapp := app{
db: db,
  }

  srv := http.Server{
Addr: ":8080",
ReadTimeout:  1 * time.Second,
WriteTimeout: 1 * time.Second,
Handler:  &myapp,
  }

  log.Fatal(srv.ListenAndServe())
}

-- 
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] Meet "fatal morestack on g0" on Linux

2018-01-05 Thread shenli
Hello everyone,

I meet a strange problem when running a program on Linux. I get "fatal: 
morestack on g0" from stderr. The process is still there but does not 
respond anymore. When I use `curl 
http://ip:port/debug/pprof/goroutine?debug=1` to check the stack, but it 
halts. There is nothing useful in stderr or dmesg.

I tried pstack and got the following result. It is very strange that there 
is only one thread alive.  I have googled it but get nothing useful.  Here 
 is an issue of golang but I 
think it is not the same thing.

Does anyone meet the same problem?




-- 
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] Using database/sql in a web application

2018-01-05 Thread Manlio Perillo
Recently I have developed a few web applications in Go, using a PostgreSQL 
database, but I have yet understand the best way to use the database/sql 
package.

The simplest method seems to start a transaction in the HTTP handler and 
pass a *sql.Tx value to all the functions that need to access the database.
However I'm afraid that this method is "too" simple.

One possible problem is an HTTP handler acting as a middleware, and both 
the "middleware" an the "normal" HTTP handler needs a transaction.
Using the "simplest method", the middleware and the handler will end up 
using two distinct transaction, and this may not be what one expects.


The other solution is to do what frameworks like Django do, storing 
something like

  type Interface interface {
  func Exec(query string, ...) ...
  func Query(query string, ...) ...
  func InTransaction) bool
  }

as a per request data, including transaction state, (ab)using 
Context.WithValue.  But I suspect that this is not a good idea.


Thanks  Manlio

-- 
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] package main: Programming in Go (youtube channel)

2018-01-05 Thread aliaksandr . pliutau
Hi, my name is Alex Pliutau,

and I started my very first Youtube channel about Programming in Go 
- https://www.youtube.com/channel/UCI39wKG8GQnuzFPN5SM55qw

I write mostly Go these days. And I love to share what I’m doing. If you 
have something to ask, do not hesitate to send me a message.

Currently I am working as tech lead at Wizeline Vietnam based in Ho Chi 
Minh City.

Topics I share in this channel:
 - Programming in Go
 - Go code review
 - Solving Algorithms with Go
 - Machine learning in Go

-- 
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] Very simple, Td array

2018-01-05 Thread Tong Sun
Problem is else where, not the tr.Td[*1*]) usage.

On Thu, Jan 4, 2018 at 6:16 PM, Tong Sun  wrote:

> Sorry, it must be the end of the day and my mind is no longer working...
>
> So I have a type
>
> type Tr struct {
> Td  []*Td`xml:"http://www.w3.org/1999/xhtml td,omitempty"
> json:"td,omitempty"`
> XMLName xml.Name `xml:"http://www.w3.org/1999/xhtml tr,omitempty"
> json:"tr,omitempty"`
> }
>
> and
>
> fmt.Printf("%#v %#v\n", tr.Td, , tr.Td[*0*])
>
> will output,
>
> []*main.Td{(*main.Td)(0xc4200271d0), (*main.Td)(0xc420027220),
> (*main.Td)(0xc420027270)} &main.Td{Colspan:"", Rowspan:"", Text:...}}
>
> but why
>
> fmt.Printf("%#v %#v\n", tr.Td, , tr.Td[*1*])
>
> will give error?:
>
> panic: runtime error: index out of range
>
>
>
> I.e., how to access the second (and third) Td?
>
> Please help, my mind is no longer working. Thx.
>
>
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/
> topic/golang-nuts/heZfZ-OZOSc/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Selenium webdriver based testing in Go

2018-01-05 Thread Tong Sun
Thanks to everyone who shared their experiences!

On Fri, Jan 5, 2018 at 3:14 AM, Simon Ritchie 
wrote:

> I’ve used the Selenium Firefox plugin to test web servers written in Go.
> It’s great for end to end testing of a web server because it doesn’t know
> or care what the server is written in.  It’s only concerned with the
> resulting HTML.
>
> I recorded some web sessions using the plugin and can then play them back
> to test any changes to the code.  The sessions are recorded in plain text
> so you can edit the description and replace any variable content with
> wildcards.  For example,  one of the tests involved creating an object
> using fixed values.  The resulting page displays the result including the
> UID, so it’s mostly the same each time except for the UID.  My edited
> Selenium script checks each field but uses a wildcard for the UID.
>
> There are lots of web scripting systems that do this sort of testing, but
> Selenium is the only one I know about that has a nice easy visual interface
> and is free.
>
> --
> 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/TNwwKz_RwZg/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Is it possible to launch a Meltdown or Spectre attack with Go code?

2018-01-05 Thread Rodolfo
https://www.debian.org/security/2018/dsa-4078

2018-01-05 6:49 GMT-04:00 Wojciech S. Czarnecki :
> On Thu, 4 Jan 2018 19:35:59 -0800 (PST)
> "'Eric Johnson' via golang-nuts"  wrote:
>
>> Anyone have any insight into whether it is possible to launch a Meltdown or
>> Spectre attack using Go code?
>
> It is *not relevant* what language was used to prepare exploit. It
> always melts down to the machine code produced;
>
> The crux of both exploits is that our process' memory can be read
> by unprivileged otherwise process of other user.
>
>> On the other hand, maybe clever use of the unsafe package
>> means it is possible?
>
> Go allows for easy asm so it is possible IMO to write an exploit
> using Go tools. Not relevant though.
>
>> Eric.
>>
>
> --
> Wojciech S. Czarnecki
>  << ^oo^ >> OHIR-RIPE
>
> --
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Is it possible to launch a Meltdown or Spectre attack with Go code?

2018-01-05 Thread Wojciech S. Czarnecki
On Thu, 4 Jan 2018 19:35:59 -0800 (PST)
"'Eric Johnson' via golang-nuts"  wrote:

> Anyone have any insight into whether it is possible to launch a Meltdown or 
> Spectre attack using Go code?

It is *not relevant* what language was used to prepare exploit. It
always melts down to the machine code produced; 

The crux of both exploits is that our process' memory can be read
by unprivileged otherwise process of other user.

> On the other hand, maybe clever use of the unsafe package
> means it is possible?

Go allows for easy asm so it is possible IMO to write an exploit
using Go tools. Not relevant though.

> Eric.
> 

-- 
Wojciech S. Czarnecki
 << ^oo^ >> OHIR-RIPE

-- 
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] Underlying arrays and their slices passed to functions

2018-01-05 Thread Ian Davis
On Thu, 4 Jan 2018, at 6:55 PM, Frank Davidson wrote:
> Thanks!!! Very helpful blog post!!
> 
> So, in proc, the slice header is copied, then an entirely new array is
> created - []byte{5,6,7,8} - and the slice header copy is set to point
> at that new array, and then discarded, whereas in proc 2, the slice
> header is not reset, and so still points to the original array?
Yes

-- 
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] Selenium webdriver based testing in Go

2018-01-05 Thread Simon Ritchie
I’ve used the Selenium Firefox plugin to test web servers written in Go.  It’s 
great for end to end testing of a web server because it doesn’t know or care 
what the server is written in.  It’s only concerned with the resulting HTML.

I recorded some web sessions using the plugin and can then play them back to 
test any changes to the code.  The sessions are recorded in plain text so you 
can edit the description and replace any variable content with wildcards.  For 
example,  one of the tests involved creating an object using fixed values.  The 
resulting page displays the result including the UID, so it’s mostly the same 
each time except for the UID.  My edited Selenium script checks each field but 
uses a wildcard for the UID.

There are lots of web scripting systems that do this sort of testing, but 
Selenium is the only one I know about that has a nice easy visual interface and 
is free.

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