Git Product home page Git Product logo

goswift's Introduction

# GoSwift - Go Goodies for Swift

Bring some of the more powerful features of Go to your iOS / Swift project such as channels, goroutines, and defers.

##Features

  • Goroutines
  • Defer
  • Panic, Recover
  • Channels
    • Buffered Channels
    • Select, Case, Default
    • Closing
  • Sync Package
    • Mutex, Cond, Once, WaitGroup
  • Error type

##Example

Note that the following example and all of the examples in the examples directory originated from http://gobyexample.com and Mark McGranaghan

Go

package main

import "fmt"

func main() {
	jobs := make(chan int, 5)
	done := make(chan bool)

	go func() {
		for {
			j, more := <-jobs
			if more {
				fmt.Println("received job", j)
			} else {
				fmt.Println("received all jobs")
				done <- true
				return
			}
		}
	}()

	for j := 1; j <= 3; j++ {
		jobs <- j
		fmt.Println("sent job", j)
	}
	close(jobs)
	fmt.Println("sent all jobs")

	<-done
}

Swift

func main() {
	var jobs = Chan<Int>(buffer: 5)
	var done = Chan<Bool>()

	go {
		for ;; {
			var (j, more) = <?jobs
			if more {
				println("received job \(j!)")
			} else {
				println("received all jobs")
				done <- true
				return
			}
		}
	}

	for var j = 1; j <= 3; j++ {
		jobs <- j
		println("sent job \(j)")
	}
	close(jobs)
	println("sent all jobs")

	<-done
}

###Run an Example

Each example has a .swift and .go file that contain the same logic.

./run.sh examples/goroutines.swift
./run.sh examples/goroutines.go

##Installation (iOS and OS X)

Add the following to your Cartfile:

github "tidwall/GoSwift"

Then run carthage update.

Follow the current instructions in Carthage's README for up to date installation instructions.

The import GoSwift directive is required in order to access GoSwift features.

Add the following to your Podfile:

use_frameworks!
pod 'GoSwift'

Then run pod install with CocoaPods 0.36 or newer.

The import GoSwift directive is required in order to access GoSwift features.

###Manually

Copy the GoSwift\go.swift file into your project.

There is no need for import GoSwift when manually installing.

Contact

Josh Baker @tidwall

License

The GoSwift source code available under the MIT License.

The Go source code in the examples directory is copyright Mark McGranaghan and licensed under a Creative Commons Attribution 3.0 Unported License.

The Swift version of the example code is by Josh Baker

goswift's People

Contributors

tidwall avatar

Watchers

yetone avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.