Skip to content

Allow passing []string #331

@richiejp

Description

@richiejp

Operating System

  • Windows
  • macOS
  • Linux
  • FreeBSD
  • Android
  • iOS

What feature would you like to be added?

I want to be able to pass []string from Go and have it converted to *char[] or **char with the last element being NULL.

Why is this needed?

So that we can pass a variable length list of strings from Go without having to serialize them.

In the LocalAI stabeldiffusion backend we pass a list of option strings which are key-values. These are then used to populate a large struct. Supporting structs on all platforms could be an alternative in this case, but perhaps a more difficult one because of padding.

In LocalAI I converted to the oo []string array like so, where CString is copied from Purego:

 var keepAlive []any
 options := make([]uintptr, len(oo), len(oo)+1)
 for i, op := range oo {
   bytep := CString(op)
   options[i] = uintptr(unsafe.Pointer(bytep))
   keepAlive = append(keepAlive, bytep)
 }

 ...

 ret := LoadModel(modelFile, modelPathC, options, opts.Threads, diffusionModel)
 if ret != 0 {
   return fmt.Errorf("could not load model")
 }

 runtime.KeepAlive(keepAlive)

This appears to work and is not complicated, but was difficult to figure out having never used unsafe pointers in Go before.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions