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

How to pass java string array into golang&java list to golang slice #24

Open
bosscloud opened this issue Jan 21, 2022 · 1 comment
Open

Comments

@bosscloud
Copy link

bosscloud commented Jan 21, 2022

Hello, need your help

I use []string in golang now, so how to call it in java?

package main

import (
	"C"
	"fmt"
	"github.com/xuri/excelize/v2"
	"math/rand"
	"strconv"
)

func main() {
	var tableHeaderEn []string
	tableHeaderEn = []string{"Item", "Name", "Age", "Gender", "Project", "Time"}
	testTimeExcel2("Book3.xlsx", "Sheet1", tableHeaderEn)

	//go build -buildmode=c-shared -o excel_stream.dll
	//go build -ldflags "-s -w" -buildmode=c-shared -o excel_stream.dll excel_stream.go
}

//export testTimeExcel2
func testTimeExcel2(excelFilePath string, iterationName string, tableHeaderEn []string) {
	fmt.Println("Start generating test time excel.")
	var xlsx *excelize.File
	var err error
	if xlsx, err = excelize.OpenFile(excelFilePath); err != nil {
		xlsx = excelize.NewFile()
	}

	streamWriter, err := xlsx.NewStreamWriter(iterationName) //"Sheet1" Ctrl+Alt+Y
	if err != nil {
		fmt.Println(err)
	}

	//表头样式
	styleIdEn, err := xlsx.NewStyle(&excelize.Style{Font: &excelize.Font{Color: "#777777"}})
	if err != nil {
		fmt.Println(err)
	}

	styleIdCn, err := xlsx.NewStyle(&excelize.Style{Font: &excelize.Font{Bold: true, Family: "Times New Roman"}})
	if err != nil {
		fmt.Println(err)
	}

	//表头名称第一行
	//var tableHeaderEn []string
	//tableHeaderEn = []string{"Item", "Name", "Age", "Gender", "Project", "Time"}
	var rowIdEn = 1
	for index, item := range tableHeaderEn {
		if err := streamWriter.SetRow(cellLocation(index, rowIdEn), []interface{}{excelize.Cell{StyleID: styleIdEn, Value: item}}); err != nil {
			fmt.Println(err)
		}
	}

	//隐藏第一行
	err = xlsx.SetRowVisible(iterationName, rowIdEn, false)
	if err != nil {
		fmt.Println(err)
	}

	//表头名称第二行
	var tableHeaderCn []string
	tableHeaderCn = []string{"序号", "姓名", "年龄", "性别", "工程", "时长"}
	var rowIdCn = 2
	for index, item := range tableHeaderCn {
		if err := streamWriter.SetRow(cellLocation(index, rowIdCn), []interface{}{excelize.Cell{StyleID: styleIdCn, Value: item}}); err != nil {
			fmt.Println(err)
		}
	}

	//第三行开始
	for rowID := 3; rowID <= 10; rowID++ {
		row := make([]interface{}, 6)
		for colID := 0; colID < 6; colID++ {
			row[colID] = rand.Intn(640000)
		}
		cell, _ := excelize.CoordinatesToCellName(1, rowID)
		if err := streamWriter.SetRow(cell, row); err != nil {
			fmt.Println(err)
		}
	}

	index := xlsx.GetSheetIndex(iterationName)
	xlsx.SetActiveSheet(index)

	if err := streamWriter.Flush(); err != nil {
		fmt.Println(err)
	}

	fmt.Println("End generating test time excel.")
	if err := xlsx.SaveAs(excelFilePath); err != nil {
		fmt.Println(err)
	}
	//return //xlsx.SaveAs(excelFilePath)
}

// cellLocation get cell location with given column and row
func cellLocation(c int, r int) (cellLocation string) {
	sc := 'A'
	if c < 26 {
		cellLocation = string(int(sc)+c) + strconv.Itoa(r)
	} else {
		cellLocation = "A" + string(int(sc)+c-26) + strconv.Itoa(r)
	}
	return
}

excel_stream.h

/* Code generated by cmd/cgo; DO NOT EDIT. */

/* package strong-excelize/hellp */


#line 1 "cgo-builtin-export-prolog"

#include <stddef.h> /* for ptrdiff_t below */

#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H

#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
#endif

#endif

/* Start of preamble from import "C" comments.  */




/* End of preamble from import "C" comments.  */


/* Start of boilerplate cgo prologue.  */
#line 1 "cgo-gcc-export-header-prolog"

#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H

typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;

/*
  static assertion to make sure the file is being used on architecture
  at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];

#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef _GoString_ GoString;
#endif
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;

#endif

/* End of boilerplate cgo prologue.  */

#ifdef __cplusplus
extern "C" {
#endif

extern __declspec(dllexport) void testTimeExcel2(GoString excelFilePath, GoString iterationName, GoSlice tableHeaderEn);

#ifdef __cplusplus
}
#endif

Thanks again

@aleqsss
Copy link

aleqsss commented Feb 27, 2023

Hi @bosscloud,

I also use a []string in GOLang and am wondering how to call it from C# rather than Java as you try. Did you ever manage to solve this, and if so, how?

Any input appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants