Skip to content

Latest commit

 

History

History

simple-c-code

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

SIMPLE C CODE EXAMPLE

A very simple example to show you how to write a c function in go.

Table of Contents

OVERVIEW

Here is a very simple c code example,

int Add(int a, int b){
    return a+b;

To implement in go, use import "C",

package main

/*
int Add(int a, int b){
    return a+b;
}
*/
import "C"
import "fmt"

func main() {

    a := C.int(10)
    b := C.int(20)
    c := C.Add(a, b)
    fmt.Printf("I'm printing the sum from go: %v\n", c)

}

RUN

go run main.go

The output is,

I'm printing the sum from go: 30