Golang 中关于 cgo 的文档比较少,所以我绝对写个简单的案例,这里用 Mysql 做案例,一种是调用C里边的函数,一种是完全用 golang 来点用 mysql 库函数。我的环境是 MAC ,如果测试的话,请 LDFLAGS 对应自己的环境编写。

注意的地方

Golang 的 cgo 引用必须单独一行,也就是说必须如下写法

import "C"

cgo 中 没有 C.NULL 类型 ,所以你要使用 nil

package main

/* 

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "/usr/local/mysql/include/mysql.h"

MYSQL *mysql,*res;
MYSQL_RES *results;
MYSQL_FIELD *field;

    struct Person {
        char *name;
        int age;
        int height;
        int weight;
    };
#cgo darwin LDFLAGS: -I/usr/local/mysql/include -L/usr/local/mysql/lib  -lmysqlclient

void get_mysql_query()
{
   MYSQL_ROW row; 
   mysql = mysql_init(NULL);
   mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client");

   res = mysql_real_connect(mysql, "127.0.0.1","root","dgj99349", "myblog", 0,NULL,0);

   if ( mysql_query(res, "SELECT * FROM wp_users")){
        fprintf(stderr,"Query failed (%s)/n",mysql_error(res));
        exit(1);
   }

   if (!(results=mysql_store_result(res))) {
        fprintf(stderr,"Couldn't get result from %s/n", mysql_error(res));
        exit(1);
    }
   
   int number = mysql_num_fields(results);
   while( ( row = mysql_fetch_row(results) ) ) {
      for ( int i = 0; i < number; i++ )
      {
         if( (field = mysql_fetch_field(results) )!=NULL ){
            printf("field_name is : %s , field_value is : %s \n", field->name,row[i]);
         }
      }
   }
   
   mysql_free_result(results);
   mysql_close(mysql);
   mysql_server_end();

   return;
}
*/ 
import "C" 
import "fmt"
import "unsafe"

const (
    maxSize = 1 << 20
)

func main() {
  
   
    C.puts(C.CString(" C MYSQL 函数查询…… ")) 
    C.get_mysql_query() 

    C.puts(C.CString(" C MYSQL 使用库函数查询…… ")) 
    // 使用C的函数库 初始化 MYSQL *
    mysql := C.mysql_init(nil);
    // 使用库连接 MYSQL *
    C.mysql_real_connect(mysql, C.CString("127.0.0.1"),C.CString("root"),C.CString("dgj99349"), C.CString("myblog"), 0,nil,0);
    // 查询函数 int
    C.mysql_query(mysql,C.CString("SELECT * FROM wp_users"))
    // 查询结果  MYSQL_RES * 
    results := C.mysql_store_result(mysql)
    // 查询的字段数目  unsigned int
    number := C.mysql_num_fields(results);
    // 查询结果 char **MYSQL_ROW
    row := C.mysql_fetch_row(results)
    // 查询的字段结果  MYSQL_FIELD *
    field := C.mysql_fetch_field(results) 
    // 每个字段的内容的长度 unsigned long
    sql_lengths := C.mysql_fetch_lengths(results)

    lengths := (*[maxSize]uint64)(unsafe.Pointer(sql_lengths))
    
    cfields := (*[maxSize]C.MYSQL_FIELD)(unsafe.Pointer(field))
    
    rowPtr := (*[maxSize]*[maxSize]byte)(unsafe.Pointer(row))
    
    for i := 0; i < int(number); i++ {
        length := cfields[i].name_length
        fname := (*[maxSize]byte)(unsafe.Pointer(cfields[i].name))[:length]
        fmt.Print("field_name is :",string(fname))
        fmt.Println(" ,field_value is : ",string(rowPtr[i][:lengths[i]]))
    }
    // 释放结果
    C.mysql_free_result(results);
    // 关闭mysql
    C.mysql_close(mysql);
    C.mysql_server_end();
   
}

运行结果如下:

QQ20160201-0

点赞(3) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部