libfact

package module
v0.0.0-...-542a020 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

README

libgofact

This project shows a simple C++ code to compute the factorial of n, and how it can be called from Go Go (golang) using SWIG

Factorial example

For this example, we'll use fact.h, fact.cpp, and fact.i.

File fact.h
#ifndef FACTORIAL_H
#define FACTORIAL_H

unsigned long long factorial(int n);

#endif

File fact.cpp
#include "fact.h"

unsigned long long factorial(int n) {
    unsigned long long result = 1;
    for(int i = 1; i <= n; ++i) {
        result *= i;
    }
    return result;
}

File fact.i

%module libfact

%{
#include "fact.h"
%}

%include "fact.h"

Steps to build locally

  1. Copy the files into a directory and change to that directory. In this case I'll use libgofact
  2. Generate the SWIG wrapper files. Run the following command in your terminal:
swig -c++ -go -cgo -intgosize 64 fact.i
  1. Copy the directory to $GOPATH/src
cp -r libgofact $GOPATH/src
cd $GOPATH/src/libgofact
go install -x
  1. Now you can use libgofact like any other package. Here it is an example:
package main

import (
	"fmt"

	"libgofact"
)

func main() {
	fmt.Println(libfact.Factorial(5))
}

Steps to build on GitHub

  1. Copy the files into a directory and change to that directory. In this case I'll use libgofact
  2. Generate the SWIG wrapper files. Run the following command in your terminal:
swig -c++ -go -cgo -intgosize 64 fact.i
  1. Push your directory to GitHub: github.com/gtoscano/libmyfact
  2. Install the package
	go get github.com/gtoscano/libgithubfact
    cd $GOPATH/src/github.com/gtoscano/libgfact
	go install -x
  1. Now you can use libmyfact like any other package. Here it is an example:
package main

import (
	"fmt"

	"github.com/gtoscano/libmyfact"
)

func main() {
	fmt.Println(libfact.Factorial(5))
}

## 



#####Example Application Code that uses simplelib.go and simplelib_wrap.cxx

You an also look at the unit tests in `simplelib_test.go`.

```go
package main

import (
	"fmt"
	"github.com/zacg/simplelib"
)

func main() {

	simpleClass := simplelib.NewSimpleClass()
	result := simpleClass.Hello()
	fmt.Println(result)

	strings := simplelib.NewStringVector()
	simpleClass.HelloString(strings)

	var i int64
	for i = 0; i < strings.Size(); i++ {
		fmt.Println(strings.Get(int(i)))
	}

	bytes := simplelib.NewByteVector()
	simpleClass.HelloBytes(bytes)

	for i = 0; i < bytes.Size(); i++ {
		fmt.Printf("%c", bytes.Get(int(i)))
	}
	fmt.Println("")

}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Swig_escape_always_false bool
View Source
var Swig_escape_val interface{}

Functions

func Factorial

func Factorial(arg1 int) (_swig_ret uint64)

func Swig_free

func Swig_free(arg1 uintptr)

func Swig_malloc

func Swig_malloc(arg1 int) (_swig_ret uintptr)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL