惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 司徒正美
N
News | PayPal Newsroom
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Cloudflare Blog
S
Secure Thoughts
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 【当耐特】
博客园 - 聂微东
S
Securelist
宝玉的分享
宝玉的分享
爱范儿
爱范儿
IT之家
IT之家
T
The Exploit Database - CXSecurity.com
S
SegmentFault 最新的问题
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
AI
AI
Security Latest
Security Latest
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Last Watchdog
The Last Watchdog
月光博客
月光博客
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
人人都是产品经理
人人都是产品经理
Webroot Blog
Webroot Blog
Jina AI
Jina AI
阮一峰的网络日志
阮一峰的网络日志
J
Java Code Geeks
N
News and Events Feed by Topic
Recent Commits to openclaw:main
Recent Commits to openclaw:main
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
S
Security Affairs
美团技术团队
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
罗磊的独立博客
Spread Privacy
Spread Privacy
Help Net Security
Help Net Security
T
Tailwind CSS Blog
C
Cybersecurity and Infrastructure Security Agency CISA
博客园_首页
Apple Machine Learning Research
Apple Machine Learning Research

Kevin Blog

产品随想 2 产品力与人才密度 产品随想 1 做营销还是做产品 Play PyTorch Stable Diffusion and ONNX, Ollama on Intel Core Ultra 5 225H Ubuntu 25.04 Play with ROCm, PyTorch, Ollama on Ubuntu 24.04 and 780m 写在新的旅程开始前 想念自然与青春 懒猫微服体验——自由协作的神器 没有光纤的日子怎么上网?自制 Home WI-FI! Swift on Server Tour 6 关联 User 和 Post Swift on Server Tour 5 创建 Users Swift on Server Tour 4 构建 Post Controller Swift on Server Tour 3 构建 Post 的 API 将不懂的日语一拍扫尽,介绍捧读全新的「OCR 工作台」功能 Swift on Server Tour 2 连通你的数据库与服务器 Swift on Server Tour 1 你的第一个 Server App 以及它背后的故事 Swift on Server Tour 0: 为什么这可能是你的好选择 纪念左耳朵耗子 How to learn Japanese by reading Novels and News with the help of Oyomi. Write WebAssembly in Swift and use it in Swift App BenQ WiT ScreenBar Halo 体验报告 记录 2021 年考驾照的体验 捧读的 EPUB 日语轻小说阅读器来了 使用 Kotlin Native 开发跨平台 Library 小番茄 - 一个只有陪伴的自习室 保友金豪电脑椅异响维修记 青岛度假指南 2020 「捧读」更新,支持针对任意文本进行语法讲解 一次百元耳机与千元耳机的横评 高刷墨水屏的海信 A5 Pro 体验报告 「捧读」更新 Safari 扩展,轻松阅读日语网页 新 App 「捧读:日语语法学习与分析」的开发幕后思考 明基 WiT ScreenBar Plus 体验 Switch游戏推荐:「健身环大冒险」和「太鼓达人」 妥协是否是一种艺术? 「50 音起源」1.2 更新说明 作客 UX Coffee 分享了自己最近的独立开发者生活 青岛度假指南 2019 我有「快乐自由」 关于小电台这款产品 聊一聊 50 音起源的再设计 极端精英主义的崩塌 Xcode Error: CFBundleSupportedPlatforms or Mach-O LC_VERSION_MIN 自由职业一周年我收获了什么 「50音起源」从起源学习 50 音的 App iOS/Android 青岛疗养(度假)指南 关于独处的一些经验 跨省携猫🐱搬家记 关于 FilmMentor 这款小众产品的想法 选择离开北京,以及为什么可以
使用 Go Mobile 开发跨平台 Library
2021-06-22 · via Kevin Blog

前情提要:

使用 Kotlin Native 开发跨平台 Library

为什么使用 Go Mobile

相对于 Kotlin Native 而言,Go 有更完善的生态支持,更小的二进制体积。

虽然 Go Mobile 维护者有跑路的嫌疑,但通过第三方的 Fork 我们已经可以支持 Apple Silicon 和 Catalyst.

下面还是用和使用 Kotlin Native 开发跨平台 Library里一样的 NASA API 来做一个 SDK,看看使用体验如何。

创建 Go Module

你可以到 gomobile-lib-demo 下载已经完成的项目

首先定义 GOPATH,我选了用户 zhoukaiwen 目录的 golang 文件夹。

export GOPATH=/Users/zhoukaiwen/golang

找个 GOPATH 之外的地方,创建 module 文件夹

mkdir go_lib_demo
cd go_lib_demo
go mod init Hello

编辑 go.mod 增加一个好用点的 HTTP 库 resty 依赖,以及解决了 Catalyst 和 Apple Silicon 的第三方 gomobile github.com/ydnar/gomobile

module Hello

go 1.16

require (
github.com/go-resty/resty/v2 v2.6.0
golang.org/x/mobile v0.0.0-20210614202936-7c8f154d1008 // indirect
)

replace golang.org/x/mobile v0.0.0-20210614202936-7c8f154d1008 => github.com/ydnar/gomobile v0.0.0-20210301201239-fb6ffafc9ef9

获取依赖

go get github.com/go-resty/resty/v2
go get golang.org/x/mobile/cmd/gomobile
go get golang.org/x/mobile/bind

初始化 gomobile

创建 API

使用 NASA 的 API 获得 Astronomy Picture of the Day 的 JSON 数据.

https://api.nasa.gov/planetary/apod?api_key={API_KEY}

src/hello/Nasa.go

package hello

import (
"encoding/json"
"fmt"

"github.com/go-resty/resty/v2"
)

type APOD struct {
Date           string `json:"date"`
Explanation    string `json:"explanation"`
HDurl          string `json:"hdurl"`
MediaType      string `json:"media_type"`
ServiceVersion string `json:"service_version"`
Title          string `json:"title"`
Url            string `json:"url"`
}

type NasaPath string

const (
nasaBaseURL NasaPath = "https://api.nasa.gov"
)

var (
apodPath NasaPath = "/planetary/apod"
)

func (m NasaPath) fullPath() string {
return fmt.Sprint(nasaBaseURL + m)
}

type NasaClient struct {
ApiKey string
}

func (nasaClient *NasaClient) GetAPOD() (*APOD, error) {
url := apodPath.fullPath()

client := resty.New()

resp, err := client.R().
SetQueryParams(map[string]string{
"api_key": nasaClient.ApiKey,
}).
Get(url)

if err != nil {
return nil, err
}

var apod APOD
if err := json.Unmarshal([]byte(resp.Body()), &apod); err != nil {
return nil, err
}

return &apod, nil
}

编译出 xcframework

gomobile bind -target ios ./src/hello 

创建 Swift Package

方法和使用 Kotlin Native 开发跨平台 Library一样,你可以到 go_lib_swift_package_demo 查看已经完工的项目。

import PackageDescription

let package = Package(
    name: "go_lib_swift_package_demo",
    products: [
        .library(
            name: "go_lib_swift_package_demo",
            targets: ["go_lib_swift_package_demo", "HappyNasa"]),
    ],
    targets: [
        .target(
            name: "go_lib_swift_package_demo",
            dependencies: []),
        .binaryTarget(
                    name: "HappyNasa",
                    path: "Sources/hello.xcframework"),
        .testTarget(
            name: "go_lib_swift_package_demoTests",
            dependencies: ["go_lib_swift_package_demo"]),
    ]
)

在 iOS 中使用

你可以到 go_lib_ios_demo 查看已经完工的项目。

import UIKit
import Hello

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let client = HelloNasaClient()
        client.apiKey = "{API_KEY}"

        do {
            let apod = try client.getAPOD()
            print(apod.title)
            print(apod.explanation)
        } catch let error {
            print(error.localizedDescription)
        }
    }
}

后记

相对于 Kotlin Native,Go 编译出的 SDK 要小很多,但比较遗憾的是 go func 在 Go Mobile 中并不能使用。如果你在函数里使用了 go func, 在编译后这个函数会被自动删除。