There is a type:

CREATE TYPE ComprositiontTransfer AS ( Id BIGINT, Name CHARACTER VARYING, COUNT BIGINT, Price NUMERIC, IdExport CHARACTER VARYING, IdImport CHARACTER VARYING ); 

In the table:

 CREATE TABLE "Transfer" ( "Id" serial NOT NULL, "Date" TIMESTAMP WITHOUT TIME zone NOT NULL, "OrgPostav" CHARACTER VARYING NOT NULL, "NameSklad" CHARACTER VARYING NOT NULL, "Сomposition" ComprositiontTransfer[] NOT NULL, "Operator" CHARACTER VARYING NOT NULL, "Total" NUMERIC, "Changed" BOOLEAN, "OneCid" CHARACTER VARYING, CONSTRAINT Transfer_pk PRIMARY KEY ("Id") ) WITH ( OIDS=FALSE ); 

Now go to go:

 type Rez struct { Id int64 Date time.Time OrgPostav string NameSklad string Сomposition СompositionS Operator string Total float64 } type СompositionS struct { Id int64 Name string Count int64 Price float64 } Сам запрос: `"SELECT \"Id\", \"Date\", \"OrgPostav\", \"NameSklad\", \"Сomposition\", \"Operator\", \"Total\" FROM \"Prixod\" LIMIT $1 OFFSET $2"` Rows, err := Requests.Query("qPrixodSelectByAll", 10, 0) /*этот запрос получает весь список*/ if err != nil { println(err) } for Rows.Next() { var ss Rez Rows.Scan(&ss.Id, &ss.Date, &ss.OrgPostav, &ss.NameSklad, &ss.Сomposition, &ss.Operator, &ss.Total) fmt.Println(ss.Сomposition) } 

Duck here in the structure ss.Composition emptiness. How to do right?

    1 answer 1

    You have several comments and errors in your code. First, initialisms do not meet the recommendations , Id must be ID .

    Secondly, you have mixed Russian and English letters in the code, for example, in Сomposition capital С is Russian.

    Third, you are trying to load what is defined in the SQL code as an array of structures into one structure.

    Fourthly, drivers do not know how to put postgres structures directly into dead structures, for this it is necessary to satisfy the database/sql.Scanner . Here is an example for a single Composition :

     func (c *Composition) Scan(src interface{}) error { log.Println(string(src.([]byte))) s := string(src.([]byte)) // Убираем скобки. s = s[1 : len(s)-1] // Разделяем. parts := strings.Split(s, ",") // Парсим части. var err error c.ID, err = strconv.Atoi(parts[0]) if err != nil { return err } c.Name = parts[1] c.Count, err = strconv.Atoi(parts[2]) if err != nil { return err } c.Price, err = strconv.ParseFloat(parts[3], 64) if err != nil { return err } return nil } 

    If you still want to load the array immediately, then it will be easier in the request to take to_json("Composition") , and in go you will determine the type of Compositions , which can read itself from the database via JSON:

     type Compositions []Composition func (c *Compositions) Scan(src interface{}) error { b := src.([]byte) err := json.Unmarshal(b, c) if err != nil { return err } return nil } 
    • Thank you, I can assure you that C is not Russian there =) Apparently here, as it were, it skipped over =) - Oma