Skip to content

Instantly share code, notes, and snippets.

View copyninja's full-sized avatar

Vasudev Kamath copyninja

View GitHub Profile

Cgo with C pointer

I've been experimenting with cgo these days to avoid writing C code. I have some shared libraries from C which I want to use in the go programs. So I was trying the passing pointer to C function from Go and got a panic! So let the code do the talking below is the code I used.

#ifndef __POINTER_H__
#define __POINTER_H__
 
 
@copyninja
copyninja / gist:9045137
Created February 17, 2014 05:18
Sample go program for cgo testing
package main
/*
#include "pointer.h"
*/
import "C"
import "fmt"
type SStruct struct {
#include <string.h>
#include "pointer.h"
void GetSampleStruct(SampleStruct *t){
SampleStruct temp = {1,2,3.0};
memcpy(t, &temp, sizeof(SampleStruct));
return;
}
#ifndef __POINTER_H__
#define __POINTER_H__
typedef struct {
int a;
int b;
float c;
} SampleStruct;
(import [os [popen]]
[os.path [exists]]
[sys]
[re])
(defun get-font-metadata [ fontfile]
"Given the font file function checks for its existence then runs the
otfinfo tool from lcdf-typetools package to extract metadata
information and returns the parsed dict."
(if (not (exists fontfile))
@copyninja
copyninja / gist:6521538
Last active December 22, 2015 19:39
Make is intelligent enough to generate compile rules on its own!!!
CFLAGS := -Wall -m32 -march=i386 -g -Iinclude1 -Iinclude2
LDFLAGS := -Llibrary-dir1 -Llibrary-dir2
FIRST_BINARY_SRC := FirstBinary.c
FIRST_BINARY_OBJ := $(FIRST_BINARY_SRC:.c=.o)
FIRST_BINARY := FirstBinary
SECOND_BINARY_SRC := SecondBinary.c
SECOND_BINARY_OBJS := $(SECOND_BINARY_SRC:.c=.o)
SECOND_BINARY := SecondBinary
(import [glob [glob]]
[os.path [expanduser]])
(defmacro pattern-glob [folder pattern]
(quasiquote (
(let [[folderpath (expanduser (unquote folder))]]
(if (not (.endswith folderpath "/"))
(setv folderpath (+ folderpath "/")))
(fn [] (glob (+ folderpath
(unquote pattern))))))))
package main
import (
"fmt"
)
func main() {
normalString := "ವಾಸುದೇವ"
backquotedString := `ವಾಸುದೇವ`
-*- mode: compilation; default-directory: "c:/Users/invakam2/Documents/Workspace/Go/src/" -*-
Compilation started at Tue Aug 13 13:57:59
go run test_strings.go
Len of normalString 21
len of backquotedString 21
Normal string with range operator yields following:
Unicode value: cb5, rendered value: ವ
Unicode value: cbe, rendered value: ಾ
Unicode value: cb8, rendered value: ಸ
/* Exercise: Loops and Functions #43 */
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := float64(2.)