Golang in the CGO use method, call the MySQL library as a case

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Golang about CGO documents relatively few, so I definitely write a simple case, here with MySQL to do case, one is to call C inside the function, one is completely with the Golang to point with the Mysql library function. My environment is the MAC, if the test, please LDFLAGS correspond to their own environment to write.

Note the place

Golang's CGO reference must be a separate line, meaning the following must be written

import "C"

There is no c.null type in CGO, so you have to use 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-lmysqlclientvoid 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_val         UE 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 <<) func main () {c.puts (c.cstring ("C MYS QL function Query ... ")) C.get_mysql_query () c.puts (c.cstring (" C MySQL use library function query ...    ")/////Use C's function library to initialize mysql * mysql: = c.mysql_init (nil); Use the library to connect MySQL * c.mysql_real_connect (MySQL, c.cstring ("127.0.0.1"), c.cstring ("root"), c.cstring ("dgj99349"), C.    CString ("MyBlog"), 0,nil,0); query function int c.mysql_query (mysql,c.cstring ("SELECT * from Wp_users"))//Query result mysql_res * Results: = C.mysql_sto    Re_result (MySQL)//query number of fields unsigned int numbers: = C.mysql_num_fields (results); Query result char **mysql_row ROW: = C.mysql_fetch_row (results)//query field result mysql_field * Field: = C.MYsql_fetch_field (Results)//The length of the contents of each field 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]))}//Release result C.mysql_free_result (results);    Turn off MySQL c.mysql_close (MySQL);   C.mysql_server_end (); }

The results of the operation are as follows:

Without permission, may not reprint this station any article: the Micro degree network»golang in the CGO use method, calls the MySQL library as the case

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.