package main
import (
"fmt"
"time"
)
var c = make(chan *int, 5)
var data = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
func main() {
fmt.Println("Hello, playground")
go save()
go read()
time.Sleep(3 * time.Second)
}
func save() {
for _, val := range data {
c <- &val
fmt.Printf("write: %v\n", &val)
}
}
func read() {
for {
val := <-c
fmt.Println("read:", *val)
}
}
Hello, playground
write: 0xc000094000
write: 0xc000094000
write: 0xc000094000
write: 0xc000094000
write: 0xc000094000
write: 0xc000094000
...
func save() {
for _, val := range data {
v := val
c <- &v
fmt.Printf("write: %v\n", &v)
}
}
Hello, playground
write: 0xc000094000
write: 0xc000094010
write: 0xc000094018
write: 0xc000094020
write: 0xc000094028
write: 0xc000094030
psql -h localhost -p 5432 -U postgres productdb
package main
import (
"context"
"fmt"
"os"
"github.com/jackc/pgx/v4"
)
func main() {
cfg := "postgres://username:password@localhost:5432/database_name"
conn, err := pgx.Connect(context.Background(), cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
defer conn.Close(context.Background())
var name string
var weight int64
err = conn.QueryRow(context.Background(), "select name, weight from widgets where id=$1", 42).Scan(&name, &weight)
if err != nil {
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
os.Exit(1)
}
fmt.Println(name, weight)
}
// тут Update
// тут Select
// потом Scan результатов Select'a
for res_answer_active_status.Next() {
err36 := res_answer_active_status.Scan(&ucp.Answer, &ucp.Asker, &ucp.Closed)
if err36 != nil {
panic(err36)
}
// и вывод результата
fmt.Println("Got new row from database")
spew.Dump(ucp)
}