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

推荐订阅源

SecWiki News
SecWiki News
I
InfoQ
The Cloudflare Blog
人人都是产品经理
人人都是产品经理
博客园 - Franky
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
博客园_首页
罗磊的独立博客
V
V2EX
李成银的技术随笔
大猫的无限游戏
大猫的无限游戏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
True Tiger Recordings
Vercel News
Vercel News
Cyberwarzone
Cyberwarzone
Cisco Talos Blog
Cisco Talos Blog
F
Fox-IT International blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
M
Microsoft Research Blog - Microsoft Research
Know Your Adversary
Know Your Adversary
爱范儿
爱范儿
The Register - Security
The Register - Security
G
Google Developers Blog
The Hacker News
The Hacker News
Malwarebytes
Malwarebytes
S
Securelist
博客园 - 三生石上(FineUI控件)
Jina AI
Jina AI
T
Threat Research - Cisco Blogs
T
The Exploit Database - CXSecurity.com
S
SegmentFault 最新的问题
博客园 - 叶小钗
F
Fortinet All Blogs
Apple Machine Learning Research
Apple Machine Learning Research
宝玉的分享
宝玉的分享
博客园 - 聂微东
T
Threatpost
博客园 - 【当耐特】
D
Docker
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
G
GRAHAM CLULEY
V
Visual Studio Blog
C
Cisco Blogs
IT之家
IT之家
S
Security Archives - TechRepublic
Latest news
Latest news
阮一峰的网络日志
阮一峰的网络日志

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 小番茄 - 一个只有陪伴的自习室
使用 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

gomobile init

创建 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, 在编译后这个函数会被自动删除。