1
0
mirror of https://github.com/golang/go synced 2024-11-14 20:40:32 -07:00
go/misc/cgo/test/issue6907.go
Ian Lance Taylor 918396b3e1 cmd/cgo: permit passing string values directly between Go and C
Permit the C preamble to use the _GoString_ type. Permit Go code to
pass string values directly to those C types. Add accessors for C
code to retrieve sizes and pointers.

Fixes #6907

Change-Id: I190c88319ec88a3ef0ddb99f342a843ba69fcaa3
Reviewed-on: https://go-review.googlesource.com/70890
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
2017-11-15 03:36:54 +00:00

34 lines
645 B
Go

// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cgotest
/*
#include <stdlib.h>
#include <string.h>
char* Issue6907CopyString(_GoString_ s) {
size_t n;
const char *p;
char *r;
n = _GoStringLen(s);
p = _GoStringPtr(s);
r = malloc(n + 1);
memmove(r, p, n);
r[n] = '\0';
return r;
}
*/
import "C"
import "testing"
func test6907(t *testing.T) {
want := "yarn"
if got := C.GoString(C.Issue6907CopyString(want)); got != want {
t.Errorf("C.GoString(C.Issue6907CopyString(%q)) == %q, want %q", want, got, want)
}
}