Golang – GoMobile工具用于跨平台返回结构体切片的返回类型。

huangapple go评论116阅读模式
英文:

Golang - GoMobile tool for cross platform slices of struct return type

问题

在跨平台移动应用开发的过程中,我遇到了Golang,它有一个GoMobile命令行工具,可以生成语言绑定,使得可以从Java和Objective-C调用Go函数。然而,在导出的函数/方法中使用的类型上有一些限制,可以在这里找到文档:https://godoc.org/golang.org/x/mobile/cmd/gobind#hdr-Type_restrictions

所以,对于在导出的函数中使用的数据类型中的结构体切片(结构体数组),有关支持的进展有什么想法吗?

英文:

During the journey of cross-platform mobile app development, I came across Golang which is having a GoMobile command line tool which generates language bindings that make it possible to call Go functions from Java and Objective-C. However, there are some restriction on the types used in an exported function/method as documented here: https://godoc.org/golang.org/x/mobile/cmd/gobind#hdr-Type_restrictions

So any idea about the work in progress to support slices of struct (Array of struct) in the data types used in exported functions

答案1

得分: 1

一个使用String作为示例的解决方法:

type StringCollection interface {
	Add(s string) StringCollection
	Get(i int) string
	Size() int
}

// TODO 使用泛型解决这个问题
type StringArray struct {
	items []string
}

func (array StringArray) Add(s string) StringArray {
	array.items = append(array.items, s)
	return array
}

func (array StringArray) Get(i int) string {
	return array.items[i]
}

func (array StringArray) Size() int {
	return len(array.items)
}

在Go中的使用方法:

func GetExampleStringArray() *StringArray {
	strings := []string{"example1", "example2"}
	return &StringArray{items: strings}
}

在Android中,你可以使用以下扩展将其转换为List<String>

fun StringArray.toStringList(): List<String> {
    val list = mutableListOf<String>()
    for (i in 0 until size()) {
        list.add(get(i))
    }
    return list
}

fun main() {
    GoPackage.getExampleStringArray().toStringList()
}
英文:

A workaround with String as an example:

type StringCollection interface {
	Add(s string) StringCollection
	Get(i int) string
	Size() int
}

// TODO solve this with generics
type StringArray struct {
	items []string
}

func (array StringArray) Add(s string) StringArray {
	array.items = append(array.items, s)
	return array
}

func (array StringArray) Get(i int) string {
	return array.items[i]
}

func (array StringArray) Size() int {
	return len(array.items)
}

Usage in go:

func GetExampleStringArray() *StringArray {
	strings := []string{&quot;example1&quot;, &quot;example2&quot;}
	return &amp;StringArray{items: strings}
}

On Android you can use this extension to convert it to a List&lt;String&gt;:

fun StringArray.toStringList(): List&lt;String&gt; {
    val list = mutableListOf&lt;String&gt;()
    for (i in 0 until size()) {
        list.add(get(i))
    }
    return list
}

fun main() {
    GoPackage.getExampleStringArray().toStringList()
}

答案2

得分: 0

我认为目前没有人在处理gomobile切片接口的实现。你可能想要检查一下这个方便的项目。

https://github.com/scisci/go-mobile-collection

这里有一个简单的例子,展示了如何在没有任何项目的情况下共享切片:

type MyType struct {
        Id       int64
        Name     string
}

var (
        items []MyType
)

func GetItemsCount() int {
        return len(items)
}

func GetItem(i int) *MyItem {
        if i >= 0 && i < len(items) {
                return &items[i]
        }
        return nil
}

对于线程,我通常在代码中添加互斥锁。

我们共享我们的业务逻辑(所有可以从移动本地代码中移出的内容),它运行得很完美。
使用gomobile 1.13bind模式。这是iOS/Android有效的跨平台开发的唯一可行解决方案。React很好,但占用更多空间。
我真的很喜欢Swift,但是移动应用中大约40MB的库依赖使其不适用。我们在我们的应用中使用它:https://play.google.com/store/apps/details?id=com.lonje,即将发布的iOS版本:https://apps.apple.com/us/app/lonje-anonymous-chat-video/id1215525783。

英文:

I think currently nobody works on gomobile slice interface implementation.
You might want to check this convenience project.

https://github.com/scisci/go-mobile-collection

and here is a simple example how you can share slice outside without any projects:

type MyType struct {
        Id       int64
        Name     string
}

var (
        items []MyType
)

func GetItemsCount() int {
        return len(items)
}

func GetItem(i int) *MyItem {
        if i &gt;= 0 &amp;&amp; i &lt; len(items) {
                return &amp;items[i]
        }
        return nil
}

In case of threads I'm usually adding mutex in code.

We sharing our business logic (everything what we can move out from mobile native code) and it works perfect.
Using gomobile 1.13 in bind mode. Only one WORKING solution for iOS/Android effective crossplatform development. React is good but takes more space.
Really like Swift, but ~40mb lib deps in mobile app kills it. Using it in our apps: https://play.google.com/store/apps/details?id=com.lonje and upcoming iOS release: https://apps.apple.com/us/app/lonje-anonymous-chat-video/id1215525783

huangapple
  • 本文由 发表于 2017年6月6日 13:41:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/44382115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定