package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
const (
host = "localhost"
port = 5432
user = "postgres"
password = "1234"
dbname = "test"
)
type signer struct {
lastname string
firstname string
surname string
position string
}
type organization struct {
title string
}
type category struct {
Type string
desc string
}
type test struct {
signer signer
organization organization
category category
period_type string
reporting_period string
sign_date string
created_date string
report_url string
}
func main() {
tests := []test{
test{
signer: signer{
lastname: "Иванов",
firstname: "Иван",
surname: "Иванович",
position: "Директор",
},
organization: organization{
title: "ФГУП",
},
category: category{
Type: "",
desc: "",
},
period_type: "",
reporting_period: "",
sign_date: "",
created_date: "",
report_url: "",
},
test{
signer: signer{
lastname: "Петров",
firstname: "Петр",
surname: "Петрович",
position: "Зам. директора",
},
organization: organization{
title: " ФГУП",
},
category: category{
Type: "",
desc: "",
},
period_type: "",
reporting_period: "",
sign_date: "",
created_date: "",
report_url: "",
},
}
// подключаетесь к базе
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
err = db.Ping()
if err != nil{
panic(err)
}
tx, _ := db.Begin() // начинаете транзакцию
for _, test := range tests {
fmt.Println(test)
// делаете запрос в базу (тут уже не db, а tx.Exec)
tx.Exec("INSERT INTO author (first_name, last_name, middle_name, position) VALUES ($1, $2, $3, $4)", test.signer.lastname, test.signer.firstname, test.signer.surname, test.signer.position)
tx.Exec("INSERT INTO organization (name_organization) VALUES ($1)", test.organization.title)
}
tx.Commit() // завершаете транзакцию, данные сохраняются в базе
}
package main
import(
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
const (
host = "localhost"
port = 5432 user = "postgres"
password = "2887"
dbname = "productdb"
)
type signer struct {
lastname string
firstname string
surname string
position string
}
type organization struct {
title string
}
type category struct {
type string
desc string
}
type test struct{
signer signer
organization organization
category category
period_type string
reporting_period string
sign_date string
created_date string
report_url string
}
func main() {
users: = []test{
test{
signer: signer {
lastname: "",
firstname: "",
surname: "",
position: "",
},
organization: organization{
title: "",
},
category: category{
type: "",
desc: "",
},
period_type: "",
reporting_period: "",
sign_date: "",
created_date: "",
report_url: "",
},
test{
signer: signer {
lastname: "",
firstname: "",
surname: "",
position: "",
},
organization: organization{
title: "",
},
category: category{
type: "",
desc: "",
},
period_type: "",
reporting_period: "",
sign_date: "",
created_date: "",
report_url: "",
},
}
}
// подключаетесь к базе
psqlInfo: = fmt.Sprintf("host=%s port=%d user=%s " +
"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, err: = sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
tx, _: = db.Begin() // начинаете транзакцию
for _, user: = range users {
fmt.Println(user)
// делаете запрос в базу (тут уже не db, а tx.Exec)
tx.Exec("INSERT INTO users (name, category, age) VALUES ($1, $2, $3)", user.UserName, user.Category, user.Age)
}
tx.Commit() // завершаете транзакцию, данные сохраняются в базе
}
let obj = Формат данных, который приниамает сервис
let data = Мои данные в виде weight, city1, city2
let result = формат данных, который в цикле собирается с подставлением данных из data