Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

danielgatis/go-ptrloop

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-ptrloop

Go Report Card License MIT Go Doc

A helper to iterate over unsafe pointers.

Install

go get -u github.com/danielgatis/go-ptrloop

And then import the package in your code:

import "github.com/danielgatis/go-ptrloop/ptrloop"

Example

An example described below is one of the use cases.

package main

/*
#include <stdlib.h>
#include <strings.h>

char** createArray(int n) {
  char** array = (char **) malloc(n * sizeof(char *));

  for (int i = 0; i < n; ++i) {
    array[i] = strdup("hello!");
  }

  return array;
}
*/
import "C"
import (
	"fmt"
	"unsafe"

	"github.com/danielgatis/go-ptrloop/ptrloop"
)

func main() {
	n := 3
	array := C.createArray(C.int(n))

	ptrloop.Loop(unsafe.Pointer(array), int(n), func(ptr unsafe.Pointer, i int) bool {
		s := *(**C.char)(ptr)
		fmt.Printf("%v -> %v\n", i, C.GoString(s))
		return true
	})
}
❯ go run main.go
0 -> hello!
1 -> hello!
2 -> hello!

License

Copyright (c) 2020-present Daniel Gatis

Licensed under MIT License

About

A helper to iterate over unsafe pointers.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.