Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgo error : can not convert C.uchar to byte by a func in golang #16695

Closed
warm3snow opened this issue Aug 15, 2016 · 0 comments
Closed

cgo error : can not convert C.uchar to byte by a func in golang #16695

warm3snow opened this issue Aug 15, 2016 · 0 comments

Comments

@warm3snow
Copy link

warm3snow commented Aug 15, 2016

  1. What version of Go are you using (go version)?
    go version go1.6.2 linux/amd64
  2. What operating system and processor architecture are you using (go env)?
    GOARCH="amd64"
    GOBIN=""
    GOEXE=""
    GOHOSTARCH="amd64"
    GOHOSTOS="linux"
    GOOS="linux"
    GOPATH="/opt/gopath"
    GORACE=""
    GOROOT="/usr/lib/go-1.6"
    GOTOOLDIR="/usr/lib/go-1.6/pkg/tool/linux_amd64"
    GO15VENDOREXPERIMENT="1"
    CC="gcc"
    GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
    CXX="g++"
    CGO_ENABLED="1"
  3. What did you do?
    In my program, I wrote a func UcharToByte(c, C.uchar) in which it convert the parameter C.uchar to byte and return it. But when I invoke the func, it throw an error: cannot use c (type C.uchar) as type utils.C.uchar in argument to utils.UcharToByte

Here is my code below.
a. $GOPATH/src/github.com/warm3snow/utils/utils.go

package utils
//#include <stdlib.h>
//#include <stdio.h>
import "C"

func UcharToByte(c C.uchar) byte {
    return byte(c)
}
func ByteToUchar(b byte) C.uchar {
    return C.uchar(b)
}

b. /home/xxx/workspace/test.go

package main

import "C"
import (
    "fmt"
    "github.com/warm3snow/utils"
)
func main() {
    var c C.uchar = 'm'
    var b byte = utils.UcharToByte(c)
    fmt.Printf("%c\n", b)
}
  1. What did you expect to see?
    I want to convert C.uchar to byte in golang. In the simple example above, I expect to see output "m"
  2. What did you see instead?
    I got this err: cannot use c (type C.uchar) as type utils.C.uchar in argument to utils.UcharToByte.
    And when I use utils.UcharToByte, it also throws an error.
@warm3snow warm3snow changed the title cgo error : convert C.uchar to byte in golang cgo error : can not convert C.uchar to byte by a func in golang Aug 15, 2016
@golang golang locked and limited conversation to collaborators Feb 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants