标签:
测试表
/* Navicat Premium Data Transfer Source Server : localhost Source Server Type : PostgreSQL Source Server Version : 90401 Source Host : localhost Source Database : mmc Source Schema : public Target Server Type : PostgreSQL Target Server Version : 90401 File Encoding : utf-8 Date: 08/15/2015 20:24:56 PM */ -- ---------------------------- -- Table structure for student -- ---------------------------- DROP TABLE IF EXISTS "public"."student"; CREATE TABLE "public"."student" ( "id" int4 NOT NULL DEFAULT nextval(‘student_id_seq‘::regclass), "name" varchar(255) NOT NULL DEFAULT ‘‘::character varying COLLATE "default", "age" int4 NOT NULL DEFAULT 0 ) WITH (OIDS=FALSE); ALTER TABLE "public"."student" OWNER TO "mmc"; -- ---------------------------- -- Records of student -- ---------------------------- BEGIN; INSERT INTO "public"."student" VALUES (‘1‘, ‘tom‘, ‘25‘); COMMIT; -- ---------------------------- -- Primary key structure for table student -- ---------------------------- ALTER TABLE "public"."student" ADD PRIMARY KEY ("id") NOT DEFERRABLE INITIALLY IMMEDIATE;
直接贴代码
package main import ( "fmt" "github.com/go-xorm/xorm" _ "github.com/lib/pq" "reflect" "unsafe" ) var engine *xorm.Engine func BytesToString(b []byte) string { bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) sh := reflect.StringHeader{bh.Data, bh.Len} return *(*string)(unsafe.Pointer(&sh)) } func StringToBytes(s string) []byte { sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) bh := reflect.SliceHeader{sh.Data, sh.Len, 0} return *(*[]byte)(unsafe.Pointer(&bh)) } func main() { engine, _ = xorm.NewEngine("postgres", "user=mmc password=mmc dbname=mmc host=127.0.0.1 port=5432 sslmode=disable") sql := "select * from student" rowArray, _ := engine.Query(sql) for _, row := range rowArray { for colName, colValue := range row { value := BytesToString(colValue) fmt.Println(colName, value) } } }
标签:
原文地址:http://www.cnblogs.com/ziyouchutuwenwu/p/4733046.html