Skip to content

cmd/cgo: definition of GoString is incorrect for exported functions #5548

Closed
@mirtchovski

Description

@mirtchovski
$ go version
go version devel +45468e9eae1f Wed May 22 18:51:15 2013 +0800 darwin/amd64

What steps will reproduce the problem?
Following Minux's suggested steps:

$ mkdir -p  /tmp/gopath/src/cgotest
$ cd /tmp/gopath/src/cgotest
$ cat > main.go <<EOF
package main

import (
        "fmt"
        "unsafe"
)

/*
        #include <stdlib.h>
        void my_printf(char* str);
*/
import "C"

//export hello_from_c
func hello_from_c(s string, i int) {
        fmt.Printf("From C: %s, %d\n", s, i)
}

func main() {
        cstr := C.CString("GoTest")
        defer C.free(unsafe.Pointer(cstr))
        C.my_printf(cstr)
}
EOF
$ cat > part.c <<EOF
#include <_cgo_export.h>
#include <stdio.h>
void my_printf(char *s){
        printf("From Go: %s\n", s);

        GoString d;
        d.p = "C Test";
        d.n = 7;

        // printf("C STRING: %s\n", d.p);
        // fflush(stdout);

        hello_from_c(d, 42);
}
EOF
$ GOPATH=/tmp/gopath go install cgotest
$ /tmp/gopath/bin/cgotest


What is the expected output?
From Go: GoTest
From C: C Test, 42

What do you see instead?
http://play.golang.org/p/QsokZKHkRr

Which operating system are you using?
OSX

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions