Skip to content

example: add Swift Package Manager example #71

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ last-change
*.iml
.idea
.gradle
.build

# Android Studio build and IDE configuration files.
example/bind/android/local.properties
Expand Down
17 changes: 17 additions & 0 deletions example/swift-package/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.DEFAULT: build

build: Frameworks $(wildcard Package.*) $(wildcard Sources/*/*)
swift build -c release

.PHONY: Frameworks
Frameworks: Frameworks/GetGo.xcframework

Frameworks/%.xcframework: Makefile $(wildcard Sources/*/*.go) $(shell which gomobile)
mkdir -p Frameworks
gomobile init
gomobile bind -target=ios,iossimulator,macos,maccatalyst -x -o $@ ./Sources/$*
touch $@

test: build
go test ./...
swift test
25 changes: 25 additions & 0 deletions example/swift-package/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// swift-tools-version:5.3
import PackageDescription

let package = Package(
name: "GetKit",
products: [
.library(
name: "GetKit",
targets: ["GetGo"]
),
],
targets: [
.target(
name: "GetKit"
),
.binaryTarget(
name: "GetGo",
path: "Frameworks/GetGo.xcframework"
),
.testTarget(
name: "GetKitTests",
dependencies: ["GetKit", "GetGo"]
),
]
)
13 changes: 13 additions & 0 deletions example/swift-package/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# [gomobile](https://pkg.go.dev/golang.org/x/mobile/cmd/gomobile) + [Swift Package Manager](https://swift.org/package-manager/)

Example [Go](https://golang.org/) package built into an [XCFramework](https://developer.apple.com/documentation/swift_packages/distributing_binary_frameworks_as_swift_packages) for [Swift Package Manager](https://swift.org/package-manager/), targeting iOS, macOS, and macCatalyst targets.

## Requirements

Go 1.16, Swift 5.3, and Xcode 11 or later.

## Usage

To build the XCFramework and Swift package: `make build`

To test: `make test`
22 changes: 22 additions & 0 deletions example/swift-package/Sources/GetGo/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package Go

import (
"io"
"net/http"
)

// Get makes an HTTP(S) GET request to url,
// returning the resulting content or an error.
func Get(url string) ([]byte, error) {
res, err := http.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
return io.ReadAll(res.Body)
}

// Version returns the version string for this package.
func Version() string {
return "0.0.1"
}
1 change: 1 addition & 0 deletions example/swift-package/Sources/GetKit/GetKit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Stub file so Swift Package Manager doesn’t complain
27 changes: 27 additions & 0 deletions example/swift-package/Tests/GetKitTests/GetTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import XCTest

@testable import GetGo

class GetTests: XCTestCase {
func testGet() {
var error: NSErrorPointer = nil
let response = GoGet("https://golang.org/", error)
XCTAssertNil(error)
guard let response = response else {
XCTFail("response == nil")
return
}
guard let str = String(data: response, encoding: .utf8) else {
XCTFail("str == nil")
return
}
XCTAssert(str.contains("Go"))
XCTAssert(str.contains("an open source programming language"))
XCTAssert(str.contains("https://play.golang.org"))
}

func testVersion() {
let version = GoVersion()
XCTAssertEqual(version, "0.0.1")
}
}