Skip to content

Commit cd53bf5

Browse files
committed
example/swift-package: add Swift Package Manager example
This depends on #70 (golang.org/cl/334689).
1 parent 5d9a332 commit cd53bf5

File tree

6 files changed

+105
-0
lines changed

6 files changed

+105
-0
lines changed

example/swift-package/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.DEFAULT: build
2+
3+
build: Frameworks $(wildcard Package.*) $(wildcard Sources/*/*)
4+
swift build -c release
5+
6+
.PHONY: Frameworks
7+
Frameworks: Frameworks/GetGo.xcframework
8+
9+
Frameworks/%.xcframework: Makefile $(wildcard Sources/*/*.go) $(shell which gomobile)
10+
mkdir -p Frameworks
11+
gomobile init
12+
gomobile bind -target=ios,iossimulator,macos,maccatalyst -x -o $@ ./Sources/$*
13+
touch $@
14+
15+
test: build
16+
go test ./...
17+
swift test

example/swift-package/Package.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// swift-tools-version:5.3
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "GetKit",
6+
products: [
7+
.library(
8+
name: "GetKit",
9+
targets: ["GetGo"]
10+
),
11+
],
12+
targets: [
13+
.target(
14+
name: "GetKit"
15+
),
16+
.binaryTarget(
17+
name: "GetGo",
18+
path: "Frameworks/GetGo.xcframework"
19+
),
20+
.testTarget(
21+
name: "GetKitTests",
22+
dependencies: ["GetKit", "GetGo"]
23+
),
24+
]
25+
)

example/swift-package/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# [gomobile](https://pkg.go.dev/golang.org/x/mobile/cmd/gomobile) + [Swift Package Manager](https://swift.org/package-manager/)
2+
3+
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.
4+
5+
## Requirements
6+
7+
Go 1.16, Swift 5.3, and Xcode 11 or later.
8+
9+
## Usage
10+
11+
To build the XCFramework and Swift package: `make build`
12+
13+
To test: `make test`
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package Go
2+
3+
import (
4+
"io"
5+
"net/http"
6+
)
7+
8+
// Get makes an HTTP(S) GET request to url,
9+
// returning the resulting content or an error.
10+
func Get(url string) ([]byte, error) {
11+
res, err := http.Get(url)
12+
if err != nil {
13+
return nil, err
14+
}
15+
defer res.Body.Close()
16+
return io.ReadAll(res.Body)
17+
}
18+
19+
// Version returns the version string for this package.
20+
func Version() string {
21+
return "0.0.1"
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Stub file so Swift Package Manager doesn’t complain
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import XCTest
2+
3+
@testable import GetGo
4+
5+
class GetTests: XCTestCase {
6+
func testGet() {
7+
var error: NSErrorPointer = nil
8+
let response = GoGet("https://golang.org/", error)
9+
XCTAssertNil(error)
10+
guard let response = response else {
11+
XCTFail("response == nil")
12+
return
13+
}
14+
guard let str = String(data: response, encoding: .utf8) else {
15+
XCTFail("str == nil")
16+
return
17+
}
18+
XCTAssert(str.contains("Go"))
19+
XCTAssert(str.contains("an open source programming language"))
20+
XCTAssert(str.contains("https://play.golang.org"))
21+
}
22+
23+
func testVersion() {
24+
let version = GoVersion()
25+
XCTAssertEqual(version, "0.0.1")
26+
}
27+
}

0 commit comments

Comments
 (0)