Hacker News new | past | comments | ask | show | jobs | submit login
Writing Apps in Go and Swift (youngdynasty.net)
158 points by youngdynasty 86 days ago | hide | past | web | favorite | 61 comments

I still find it hard to write maintainable multi-threaded code for macOS or iOS [...] without having to worry about the minutiae of parallelism (threads, semaphores, locks, barriers, etc.).

I find this surprising, as GCD does insulate you from that low-level stuff. When you need to work with mutable data, just create a dispatch queue, and only ever access the data by dispatching a function to the queue. Both Swift and Objective-C have friendly syntax for anonymous functions that makes this lightweight and easy.

This reads like someone who purposely missed that part of the documentation just to write this article.

I think I would have less hang ups if the author just came out and said I wanted to try using Go for multi-threaded code with Swift instead of trying to make GCD sound so confusing.

Even so, I'm glad it's been written. I didn't know you could call Go code from C code (new feature since Go 1.5 apparently) or that Swift could access compiled files with these "module map" things. I'm really glad to see they've both evolved to include useful interoperability and play nicely with other things now, and I'm glad this article shows how in a practical way.

> Swift could access compiled files with these "module map" things

That's been the case since it was released – all Swift C / ObjC interop happens at the "module" level. You write a Clang module map (if you're not using a system library which already has one), point the map at the relevant header(s), then you can import that module into your Swift code.

That said, not everything is capable of being imported (variadics, complex macros, VLA structs, and a few others).

Indeed .. things have come a long way since I tried to port IPFS to iOS ..


It seems like there's a better way to go about this than my method. I was motivated to do this not because I don't like GCD (I still don't) but because I really like Go and IPFS and didn't think that iOS should be left out of the party.

But, looking at it again, I still don't think GCD is all that great. This seems to have been intentionally designed to be different from the way so many other systems handle the issue - or at least, different enough that its still relatively discomfiting to attempt to port something away from iOS and not just to it ...

Exactly. I get that GCD can be pain to work with, but the OperationQueue class wasn't even mentioned as an alternate.

GCD is fantastic and way better than everything on macOS before it, but it's still not a silver bullet (neither is Go).

Cocoa is incredibly fragile about the main thread, so you need to be super careful what runs in what queue. If you add KVO/bindings into the mix, it needs an extraordinary level of paranoia^Wdilligence.

> Cocoa is incredibly fragile about the main thread, so you need to be super careful what runs in what queue.

This basically just boils down to "don't touch the UI off the main thread". There are some exceptions with CoreAnimation, but other than that the main thread checker will yell at you if you do something wrong.

The main thread checker is a godsend. Enabling it + TSAN + Core Data concurrency assertions have reduced the threading bugs my team deals with to almost 0.

Anywhere we're doing something specific, we make liberal use of `dispatchPrecondition(condition: .onQueue(mySerialQueue))` or `assert(Thread.isMainThread)`.

Even in places where we are doing multithreaded UI rendering (mostly CATiledLayer), it's become a non-issue. It's a night-and-day difference compared to 5+ years ago.

> Cocoa is incredibly fragile about the main thread

That basically applies to all UI framework. At least I'm not aware about one which provides good support for accessing it from another thread.

There is obviously a reason for it, which is that UI frameworks are incredibly stateful, and trying to manipulate lots of state from multiple threads at once rarely works out well.

I think BeOS tried to and it did not went well with common threading issues poping up on BeOS apps.

Not sure if Haiku has any improvements regarding it.

Not sure what you mean when you say "common threading issues"; the entire point of the Be design was to force all threads to use message-based communication instead of shared memory for virtually all uses, and mandatory locking otherwise. It's extremely effective.

It was quite common to see BeOS apps crashing due to not handling the threads communication properly.

Having the whole OS being so multithreading heavy was a novelty for the large majority of developers and quite easy to make mistakes.

I found adopting a functional reactive programming really works well for multithreaded systems.

GCD is super great and I use it a lot (...if you had to extend AsyncTask recently: my condolences), but it doesn't safeguard you from race conditions, dealing with locks and all that fun stuff.

I don't have a lot of golang experience, but from what I can see here, golang doesn't really either: https://tour.golang.org/concurrency/1

Channels seem like a simple thread safe stream for the most part, which you can get with Rx.

This is correct, golang doesn't give you thread safety for free. Values passed via channels are safe as they're implicitly locked (i.e. it's a blocking threadsafe queue), and there are happens-before semantics for starting a goroutine, otherwise you're on your own (explicit locks / atomics / lock-wrappers like sync.WaitGroup). E.g. variable assignment isn't even atomic.

> Give Emporter a try and let me know how it compares to an Electron app.

I don't see how this technique is comparable to Electron. The article does not describe anything related to a cross-platform user interface, which is what Electron addresses. You can write non-UI logic that is cross-platform in half a dozen mainstream languages and another dozen less popular ones. That's not a big deal. It's the UI component that is harder to achieve, and that is what Electron offers.

Thanks for your feedback. Honestly, I was just being sassy. It probably is like comparing apples to oranges. This technique does not concern itself with a cross-platform interface. It only makes it possible to keep the logic cross-platform so that the "only" difference is the UI implementation, which can be done using native SDKs.

Yeah, I don't understand either. Is he referring to a different 'Electron'?

Just wanted to say that module maps are absolutely not necessary to have static libraries (.a file author compiles his go program to) in your project.

All you need is an objective-c bridging header where you do an #import "name_of_header_.h" for every header. After that, the headers are visible to all of your swift code. It's no different than mixing objective-c and swift, except here you're mixing your language of choice, compiled to callable C functions inside a static library.

To recap - drag .h and .a files the same way you have .swift files into your xcode project. Add a BridgingHeader.h file, go to it, fill it with #import "name_of_header.h" statements. Lastly, the project needs to know you're using a bridging header, that's done in the project target's Build Settings tab, under "Objective-C Bridging Header" you need to have the value set to the filename you chose for your bridging header.

This is not unique to calling Go in Swift btw - any language that can be called from C, can be called from Objective-C, and therefore Swift. One thing to be aware of is memory management - unless you're passing things by value (copying), making sense of when things can be safely deallocated across languages is non-trivial.

Go has great crypto library, so I've wrapped it for use with Swift: https://github.com/lastochkanetwork/EasyPGP

Heads up: you made this error at least twice, once in the article and once in the repo. https://brians.wsu.edu/2016/05/31/complement-compliment/ It's complementary instead of complimentary. The word "complimentary" is rarely used, but could be used if you were to mention books related to complimenting people.


I don't find GCD difficult to write for, maybe if you are used to Go it is different enough. Not sure what exactly you would use Go for in an iOS/Macos app unless maybe you have a Go framework you want to include.

Isn’t this what the author is doing?

I consider GCD to be a bit of a smell - not because its difficult to write for, but rather because its difficult to port such code elsewhere. And really, this seems to be the only rationale for Apple having maintained it as a component of the architecture for iOS - to be relatively different, or just different enough, from other systems to dissuade porting the code elsewhere.

Yeah. Worth noting too that libdispatch has been open source since it was released 10 years ago. I compiled and used it on Linux before Swift was out.

Sure. The solution to not wanting to be dependent on Apple's software is .. to use more of Apples software.

Not really rational.

It’s open source. You can use it on Linux rather than being tied to MacOS or iOS. What’s your objection?

Lets try this a different way. Whats so great about GCD that other processor resource management systems get so wrong, that using GCD is the only thing that makes sense to you?

(Hint: its koolaid, kid.)

It’s not a memory management system.

Right, brain fart on the train. But the point still stands, why should we use GCD when there are other more portable means out there - which are also supported on iOS - to do the same thing? It's just different for the sake of being different.

Wikipedia says GCD and Go are the same age (both released 2009). Is Go also different for the sake of being different? Why is Go good and GCD bad?

I imagine we’re not going to convince each other of the technical merits, but I personally think GCD is a very nice programming model. I’m less of a fan of Go but plenty of people love it so clearly there’s something there. There’s room for both.

It’s a valid criticism of a lot of Apple tech that it’s very locked down, but given libdispatch is open source (as are the Clang changes to support block syntax in C) I don’t think that criticism is fair here.

Swift Strings can transparently map to UnsafePointer<CChar> when necessary; is there a reason why this doesn’t work in this case?

I found your article particularly interesting, because lately I've been experimenting with calling Swift from Go.

The approach is based on the same principle: cgo as bridge between Go and a C library. The C library is build by the Swift package manager. Blog post on Dev community with details:


How well does this work? Can you call a Go library function that spawns goroutines?

Yes! I've shipped an app, on the Mac App Store, which uses Go in a non-trivial way (including spawning goroutines). The link is in the article.

Zenly ( http://zen.ly, millions of users, bought by snapchat ) has had an app coded in go and swift for ios ( and go and java for android) for a few years. Judging by the app, and the fact that it’s cross platform i’d say the go part does all the data processing part.

https://m.youtube.com/watch?v=R0oaOohl5jk ( in french but the slides are in english)

Thanks for sharing this! As someone who writes in Go every day but stopped following it a while back, I had no idea you could even call Go from C much less Swift.

Someone that doesn't know how to write Swift, tries to find faults at all costs.

Go, horrible as it is, doesn't lack cultists.

I don't see the author pointing out any faults in Swift. There's a passing comment about GCD being complicated, which I personally disagree with, but this isn't specific to Swift.

I am go ahead and say it: Go is a replacement for Perl. It is nice for process and machine administration automation. Can you write your app in go? yes. Could you also write your app in perl cgi? sure. Did many people wrote perl cgi apps? yes. did they regret it. yes. Will people writing apps in go will regret it? who knows. Probably yes.

This comment is not about the language per-se. it is about the current goals of the people with money and weight behind the language right now. I guess the regret will come or not if those goals keep or change.

I'm in the midst of a giant Perl rewrite to Go. I see Perl as glue code, but as soon as you want a "real app" (for various definitions thereof), Perl will bite you. Go is designed for large teams and networked services with concurrency as a first class citizen.

In our Perl code base, we have had so many issues with auto-vivification, lack of argument tracing (just pass around @_ everywhere!), callback hell in AnyEvent for concurrency, and more. Maybe if you use Moose everywhere, you can get some form of sanity, but I doubt it. Engineers I have full respect for have scratched their heads trying to initially dive into this perl. What I can grant however is that it is able to do a lot of work (given enough machines!).

For the Go version, I know exact method signatures and variable types. Concurrency if first class. And just about anyone can read the code and figure out what is going on. We've onboarded new grads who can help put solid features into this already large Go codebase quickly. We are seeing 20x optimization in throughput over one code base from Perl (it requires a lot of waiting on remote servers we don't control), and 100x in another.

I can't imagine regretting the choice to write in Go for networked services running in the backend.

Go is designed for armies of simple minded developers, Java 1.0 revisited.

“The key point here is our programmers are Googlers, they’re not researchers. They’re typically, fairly young, fresh out of school, probably learned Java, maybe learned C or C++, probably learned Python. They’re not capable of understanding a brilliant language but we want to use them to build good software. So, the language that we give them has to be easy for them to understand and easy to adopt.”

-- Rob Pike

From https://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2014/Fr...

Simple minded is your words, not his (in another talk, I think he refers to these people as regular developers).

The harsh language there is not being "capable of understanding a brilliant language." I'm not aware of what was meant by a brilliant language, but I have to assume that it means research languages. I would not expect fresh grads to build production worthy code in any language, but especially not in a research languages (which typically are relegated to the realm of research because they are not capable of being used by large teams for the making of good software).

I'm in the business of creating value with good software, not ivory tower building that only a smaller proportion of software artisans can build and maintain.

Maybe simple minded is not the right way to put it, however from other talks even modern Java is considered too feature rich for such target group.

Go is designed for armies of simple minded developers,

And that's a bad thing?

Not for companies of Google size wanting to reduce hiring and ongoing project costs.

Simple minded developers are cheap. And in time, easily replaceable by moderately intelligent AI.

The crowing achievements of AI that interact with people are Alexa et al. Alexa can't even tell me what the newest releases are for movies.

When we can talk to an AI and describe something novel and it can interpret that, then I'll start to worry about software development jobs being lost to AI.

What about being lost to low wages countries, with better wage countries keeping the roles of project management and architecture?

Didn't that scare happen a while ago with everyone worried about India taking all the software jobs? Didn't really happen. Could happen again, sure. I don't think more advanced AI or more simple programming languages will be a large factor in that.

I can tell you that the trend to offshore is still pretty ongoing in full speed.

Naturally those developers are also entitled to have jobs, the question is how it is actually done in practice.

Sadly a large majority are sweetshops.

They couldn't be further from each other. Just from browsing code, they are on opposite ends of the spectrum. Go is easily readable, but at the cost of brevity. Perl is -typically- unreadable, with a lot of one liner magic. If you are stating merely can a person write a web app in both, well, I think that goes for any language in existence.

Perl could've been readable - nothing in the language prevents you from writing readable code. The problem was two-fold, imho:

1) regex was treated like the primary way to do things - even when it wasn't necessarily called for - at the expense of readability (and Perl supported it so well)

2) sysadmins

The two combined together (and possibly the fact that it was the early days of commercial internet service) led to the idea that anything done in Perl was destined to look like "line noise" to actual SWEs.

/just how I saw it...

I know. Perl was all that at the time. daunting! modern! no compiler OMG! text transformation on the code itself! it was the bee knees.

All that Go delivers today, over the medium, is very similar to what perl also delivered yesteryear, on top of yesteryear medium. But also like perl, it is being born from a bunch of old unix system engineers :)

Are you sure you're not confusing Go with some other language?

> daunting!

Go is often praised for being simple.

> modern!

...and criticized for being too simple, ignoring modern advances in programming language research.

> no compiler OMG!

The compiler and its careful tradeoff between fast compilation and fast programs is a key characteristic of Go.

> text transformation on the code itself!

Go doesn't have macros. Instead people fall back on code generation, just like Java.

I cannot rightly comprehend the confusion of ideas that led to such a statement.

Comparing Go with Perl is rather odd. Maybe you could tell us why we might regret it more than using Java or C# or Node.js?

Was perl ever the language of choice for distributed services or database systems?

Did I miss that part of history?

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact