GOLang & ( Objective-C || Swift )

I have seen lots of Front-end developers that are curious to develop something for iOS Devices but it takes time for them to jump into Swift or Objective-C. So, many are using third party libraries like Phone-gap or Xamarin or… .

Now let’s look as a iOS developer. have you ever asked for developing something that could be reusable in Android ! well, yeah I asked for it many times. wouldn’t be awesome if you develop a module and compile it for iOS and Android both same at a time! Did you import AFNetwork or PODS in your project just to have a simple POST request or parsing a JSON ?
For me GOLang was a yet another functional language until… .One of my colleges told me about making a library with GOLang for iOS and android. He was a Front-End developer in our company. He developed a module with GOLang which was reusable in both androids and iOS. We were using that for Localization, Sending requests, Parsing responses, Adding Google Analytics. So we had not to implement any connection part in our App. Seems awesome ! Well, Yeah. But! Let me finish it.

First, Let’s jump into GOLang, It’s very easy to setup and learn. It has some cool features like defer, channels ,slices …
To start it you can easily download it and install it on your Mac. I recommend you to use BREW to install GOLang. There is a good tutorial for it : SETUP GOLang .Then you need to install GOMoile which is pretty easy if you just follow it’s wiki. GOMobile.

Alright, we have everything now. Let’s just develop a simple application. You don’t need any IDE. Just a text editor would be enough. “I use ATOM with GO plugin”.

Go

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

packageMyPackage

typeCalculatorstruct{

Fnum int

Snum int

}

func(c *Calculator)Add()int{

returnc.Fnum+c.Snum

}

funcNewCalculator()*Calculator{

varnewCal=Calculator{

Fnum:0,

Snum:0,

}

return&newCal

}

Well, I know I don’t need to have Calculator structure in order to call those methods. it’s just an example to give you an idea how you can have an object and set it’s properties or calling its methods.

Done! we just need to compile it as a framework for iOS. Easy as a pie

Objective-C

1

2

gomobile build-target=ios golang.org/x/mobile/yourFilePath

So, now we have a framework file. you just need to drag it and drop it into your project. Let’s check Headers inside of our framework. I have GoMyPackage. Inside of that, you will find setters and getters for your properties and a exported method in order to allocate your object and passing it’s reference to you and That’s all we need basically.

let’s import our framework into our controller. whatever language you are using it’s not important. I use swift for this

Swift

1

2

3

4

letcal=GoMyPackageNewCalculator()!

cal.setFnum(40)

cal.setSnum(50)

print(cal?.add())

let’s back to BUT!. As I said there is a but while you want to use GOLang in your iOS Project. First of all, It’s very hard to debug your application coz you don’t know who has the issue. If you want to use it, you should make sure that your GO part works fine unless it gonna be a disaster. Anything else ? well, You should always be careful. After iOS 10 our App had a crash right after opening the app. I am not sure what was the reason of that. GO’s runtime had some issue with initializing in iOS 10 environment. Even maybe someday, later, apple would not let to use this kind of third party libraries. So it’s on you