This is a creation in Article, where the information may have evolved or changed.
<strong style= "margin:0px; padding:0px; border:0px; font-size:15px; Font-weight:bold; Color:rgb (34, 34, 34); font-family: ' Helvetica Neue ', Helvetica, Arial, Sans-serif; Font-style:normal; Font-variant:normal; Letter-spacing:normal; line-height:19px; Orphans:auto; Text-align:left; text-indent:0px; Text-transform:none; White-space:normal; Widows:auto; word-spacing:0px; -webkit-text-stroke-width:0px; Background-color:rgb (255, 255, 255); " >wrapper.c</strong> #include <stdlib.h> #include <string.h> #include <sys/shm.h> #include <sys/types.h>int My_shm_open (char* filename, int open_flag) {int shm_id; key_t key; Key = Ftok (filename, 0x03); if (key = =-1) {return-1; } if (open_flag) shm_id = Shmget (key, 4096, ipc_creat| IPC_EXCL|0600); else shm_id = shmget (key, 0, 0); if (shm_id = =-1) {return-1; } return shm_id;} int my_shm_update (int shm_id, char* content) {char* addr; addr = (char*) Shmat (shm_id, NULL, 0); if (addr = = (char*)-1) {return-1; } if (strlen (content) > 4095) return-1; strcpy (addr, content); SHMDT (addr); return 0;} int my_shm_close (int shm_id) {shmctl (shm_id, Ipc_rmid, NULL); return 0;} char* my_shm_read (char* filename) {int shm_id; char* addr; char* s; shm_id = my_shm_open (filename, 0); if (shm_id = =-1) return NULL; Addr = (char*) shmat (shm_id, NULL, 0); if (addr = = (char*)-1) {return NULL; } s = (char*) malloc (strlen (addr) + 1); strcpy (S, addr); SHMDT (addr); return s;}
<strong style= "margin:0px; padding:0px; border:0px; font-size:15px; Font-weight:bold; Color:rgb (34, 34, 34); font-family: ' Helvetica Neue ', Helvetica, Arial, Sans-serif; Font-style:normal; Font-variant:normal; Letter-spacing:normal; line-height:19px; Orphans:auto; Text-align:left; text-indent:0px; Text-transform:none; White-space:normal; Widows:auto; word-spacing:0px; -webkit-text-stroke-width:0px; Background-color:rgb (255, 255, 255); " >reader.go</strong>package main//#include <stdlib.h>//#include "wrapper.c" import "C" import "unsafe" Import "FMT" func read (filename string) string { f: = c.cstring (filename) defer c.free (unsafe. Pointer (f)) s: = C.my_shm_read (f) defer c.free (unsafe. Pointer (s)) return c.gostring (s)}func main () { fmt. Println (Read ("tmp"))}
<strong style= "margin:0px; padding:0px; border:0px; font-size:15px; Font-weight:bold; Color:rgb (34, 34, 34); font-family: ' Helvetica Neue ', Helvetica, Arial, Sans-serif; Font-style:normal; Font-variant:normal; Letter-spacing:normal; line-height:19px; Orphans:auto; Text-align:left; text-indent:0px; Text-transform:none; White-space:normal; Widows:auto; word-spacing:0px; -webkit-text-stroke-width:0px; Background-color:rgb (255, 255, 255); " >writter.go</strong>package main//#include <stdlib.h>//#include "wrapper.c" import "C" import "unsafe" Import ("Log" "Time") type errorstring struct {s string}func (e *errorstring) Error () string {return E.s}fun C Open (file string) (int, error) {f: = c.cstring (file) defer c.free (unsafe. Pointer (f)) R: = Int (C.my_shm_open (f, C.int (1))) if r = =-1 {return 0, &errorstring{"error"}} RET Urn R, nil}func update (shm_id int, content string) error {c: = c.cstring (content) defer C. Free (unsafe. Pointer (c)) R: = Int (C.my_shm_update (C.int (shm_id), c)) if r = =-1 {return &errorstring{"update Error"} } return Nil}func close (shm_id int) error {C.my_shm_close (C.int (shm_id)) return Nil}func main () {ID, err : = Open ("/tmp") if err! = Nil {log. Fatal (ERR)} defer close (id) Err = update (ID, "Hello World") if err! = Nil {log. Fatal (ERR)} time. Sleep (1E9 * 100)}This is my stackoverflower on the question others answer, or not too understand, keep.