topinvestor
(Constantine Vassil)
1
I'm developing iOS App. The backend is in Goolge Cloud.
I have web service in Golang. Google provides Golang
client libraries to work with Google Cloud services.
For example calling BigQuery and getting the results.
I'm considering using Kitura. I generated a sample Kitura
project and uploaded to to Google Cloud Run. It is working fine.
Now I want to call BigQuery from Swift. There are not Swift libraries
for Google Cloud services. There are are very good libraries for Golang.
With Gomobile it is possible to generate a framework and use it in iOS project.
Is it possible to import a framework in Server Side Swift project and thus use
existing Golang libraries?
jonprescott
(Jonathan Prescott)
2
With difficulty. If you can call the Golang libraries using C, you should be able to write Swift code to use the C-interface to interact with the Golang library, using the Swift interoperability with C. If you can't call using C-code, you will have write wrapper code to bridge between Swift and Golang, also based on C calling conventions.
jonprescott
(Jonathan Prescott)
3
Note that Gomobile does exactly this by writing out an Objective-C framework that gets compiled as part of the iOS application.
topinvestor
(Constantine Vassil)
4
Here is one example:
gomobile_hello.go
package gomobile_hello
import "fmt"
func Greetings(name string) string {
return fmt.Sprintf("Hello ver 1.1, %s!", name)
=====================================
Then from command line:
gomobile bind -target=ios ../go_myproject_gomobile
I get the following structure:
Gomobile_hello.framework
├── Gomobile_hello -> Versions/Current/Gomobile_hello
├── Headers -> Versions/Current/Headers
├── Modules -> Versions/Current/Modules
├── Resources -> Versions/Current/Resources
└── Versions
├── A
│ ├── Gomobile_hello
│ ├── Headers
│ │ ├── Gomobile_hello.h
│ │ ├── Gomobile_hello.objc.h
│ │ ├── Universe.objc.h
│ │ └── ref.h
│ ├── Modules
│ │ └── module.modulemap
│ └── Resources
│ └── Info.plist
└── Current -> A
=============
Inside iOS Project:
Import Gomobile_hello.framework in the iOS project
Then use it as follows:
import UIKit
import go_myproject_gomobile // Gomobile bind generated framework
class ViewController: UIViewController {
@IBOutlet weak var textLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let h3 = Go_myproject_gomobile_Greetings("Hi")
textLabel.text = h3
}
}
=============
I don't believe in the generated Gomobile_hello.framework there is something iOS specific.
Now - how to use Gomobile_hello.framework in Server Side Swift?
but this file is compiled and I suppose is iPhone specific: Gomobile_hello
But here:
there are another options:
New execution modes
These are the new execution modes we propose:
- Go code linked into, and called from, a non-Go program.
- Go code linked into a shared library loaded as a plugin by a program (Go or non-Go) that supports a C style plugin API.
- Go code linked into a shared library loaded as a plugin by a Go program that supports a general Go style plugin API.
- A Go program that uses a plugin interface, either C style or Go style, where plugins are implemented as shared libraries.
- Building a Go package, or collection of packages, as a shared library that may be linked into a Go program.
- A Go program built as a PIE--a Position Independent Executable.
jonprescott
(Jonathan Prescott)
5
The file "Gomobile_hello" is an iOS dynamic library with at least an ARM binary, and possibly an X86_64 binary for the iOS simulator running on macOS. Neither of which will run on Linux, for example, or on macOS (except from within the simulator in Xcode if you've targeted that in the library). What you diagrammed is classic macOS/iOS framework, based on binary dynamic libraries.
Those .h files in your Headers directory are the C(Obj-C) interface that the gomobile tool generates for your Go framework that can be called from Swift, C, Obj-C, Obj-C++, C++, etc..
Based on what you've provided, it looks like Gomobile creates a binary library/framework in that invocation. If Gomobile supports your server target architecture (-target=...), you should be in pretty good shape. But, based on what I saw about the tool on the Golang site, it only generates frameworks for iOS and Android. Unless your server target architecture is iOS or Android, you will be out of luck because of the binary incompatibility.
jonprescott
(Jonathan Prescott)
6
You could try having "gomobile" generate the header files from the Go code to use in compiling the Swift calling code, then link in the appropriate Go-generated binary library as part of building the server application, as long as you have a Go library for the target architecture. You'll have to really look at the gomobile generated and headers and see if they can be used to successfully call into the Go binary.