github.com/epfl-dcsl/gotee@v0.0.0-20200909122901-014b35f5e5e9/misc/cgo/test/issue6907.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package cgotest
     6  
     7  /*
     8  #include <stdlib.h>
     9  #include <string.h>
    10  
    11  char* Issue6907CopyString(_GoString_ s) {
    12  	size_t n;
    13  	const char *p;
    14  	char *r;
    15  
    16  	n = _GoStringLen(s);
    17  	p = _GoStringPtr(s);
    18  	r = malloc(n + 1);
    19  	memmove(r, p, n);
    20  	r[n] = '\0';
    21  	return r;
    22  }
    23  */
    24  import "C"
    25  
    26  import "testing"
    27  
    28  func test6907(t *testing.T) {
    29  	want := "yarn"
    30  	if got := C.GoString(C.Issue6907CopyString(want)); got != want {
    31  		t.Errorf("C.GoString(C.Issue6907CopyString(%q)) == %q, want %q", want, got, want)
    32  	}
    33  }