ivetkin
ivetkin

Reputation: 41

Working with USB in Go

Can somebody provide an example of using go-libusb (or may be any other lib working with USB in Golang) .

  1. I have created package libusb
  2. Copied the content from https://github.com/popons/go-libusb/blob/main/libusb.go

There is an error:

Unresolved symbol: '_Cstruct_usb_device_descriptor' 

Thank you.

Upvotes: 4

Views: 12139

Answers (2)

Matthew Rankin
Matthew Rankin

Reputation: 461157

The go-libusb package by popons is a wrapper for the libusb-0.1 C-based USB driver. libusb-0.1 is "deprecated and unmaintained." [Source: libusb API version overview]

For using USB with Go, I'd recommend using a libusb 1.0 (source on GitHub) Go-driver, such as:

  • gousb — Originally created by Kyle Lemons. There are newer forks, such as the one by truveris.
  • libusbDiscloure: I'm the original creator.

Both of the above Go drivers require having the C-version of libusb installed. To install on Windows, take a look at the libusb Windows driver installation instructions, which recommend using Zadig. You could also look at the SO question Installing libusb-1.0 on Windows 7, but the only answer appears to show how to install libusb-0.1 not libusb-1.0.

As a Windows user, I know this doesn't help you, but for OS X users, libusb can be installed using Homebrew:

$ brew install libusb

Upvotes: 3

OneOfOne
OneOfOne

Reputation: 99215

First you have to install libusb from http://www.libusb.org/wiki/libusb-win32, then either change // #include<usb.h> to:

/*
    #cgo LDFLAGS: -lusb
    #include <usb.h>
*/

And descriptor _Cstruct_usb_device_descriptor to descriptor C.struct_usb_device_descriptor.

Or pull my fork from https://github.com/OneOfOne/go-libusb, I already sent a pull request with the changes.

Upvotes: 3

Related Questions