Skip to content

Instantly share code, notes, and snippets.

@mogeta mogeta/Gomobile.md
Last active Jun 15, 2016

Embed
What would you like to do?
gomobile

gomobile bindによるライブラリ作成おさらい。

  • go getでgomobile取り込み
  • gomobile intiでNDKなどの取得
  • gomobile bind でframeworkやaarファイルを作成。
  • Android,iOS各プロジェクトに投入。動作確認
go get golang.org/x/mobile/cmd/gomobile
gomobile init
gomobile bind -target ios
gomobile bind -target android

Androidにてpermission付与わすれた時のエラー、行数なんかも表示してくれる

06-15 16:00:49.907 27451-0/sandbox.muzigen.net.abreak E/Go: panic: runtime error: invalid memory address or nil pointer dereference
06-15 16:00:49.907 27451-0/sandbox.muzigen.net.abreak E/Go: [signal 0xb code=0x1 addr=0x0 pc=0x7f923827b8]
06-15 16:00:49.907 27451-0/sandbox.muzigen.net.abreak E/Go: goroutine 17 [running, locked to thread]:
06-15 16:00:49.907 27451-0/sandbox.muzigen.net.abreak E/Go: panic(0x7f92679f60, 0x482000a070)
06-15 16:00:49.907 27451-0/sandbox.muzigen.net.abreak E/Go: 	/usr/local/Cellar/go/1.6.2/libexec/src/runtime/panic.go:481 +0x384
06-15 16:00:49.907 27451-0/sandbox.muzigen.net.abreak E/Go: github.com/mogeta/breaktime.GetURL(0x0, 0x0)
06-15 16:00:49.907 27451-0/sandbox.muzigen.net.abreak E/Go: 	/Users/h-mimori/go/src/github.com/mogeta/breaktime/status.go:52 +0x48
06-15 16:00:49.907 27451-0/sandbox.muzigen.net.abreak E/Go: github.com/mogeta/breaktime.Get(0x482002cee0)
06-15 16:00:49.907 27451-0/sandbox.muzigen.net.abreak E/Go: 	/Users/h-mimori/go/src/github.com/mogeta/breaktime/status.go:24 +0x20
06-15 16:00:49.907 27451-0/sandbox.muzigen.net.abreak E/Go: _/var/folders/t9/634g7z_x13gdbf6y8t8z2fp80000gp/T/gomobile-work-762838960/gomobile_bind.proxybreaktime__Get(0x482002cf08)
06-15 16:00:49.907 27451-0/sandbox.muzigen.net.abreak E/Go: 	/var/folders/t9/634g7z_x13gdbf6y8t8z2fp80000gp/T/gomobile-work-762838960/gomobile_bind/go_breaktimemain.go:103 +0x20
06-15 16:00:49.907 27451-0/? E/Go: _/var/folders/t9/634g7z_x13gdbf6y8t8z2fp80000gp/T/gomobile-work-762838960/gomobile_bind._cgoexpwrap_85cb1b500e13_proxybreaktime__Get(0x3e21ee9ebdb4b1c4)
06-15 16:00:49.907 27451-0/? E/Go: 	??:0 +0x28
package sandbox.muzigen.net.abreak;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import go.breaktime.Breaktime;
import static android.R.attr.button;
public class MainActivity extends AppCompatActivity {
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handler.post(new Runnable() {
@Override
public void run() {
TextView text = (TextView) findViewById(R.id.textView);
if(Breaktime.Get()){
text.setText("work! work! work!");
}else{
text.setText("beer! beer! beer!");
}
}
});
}
});
}
}
//get github status
package breaktime
import (
"encoding/json"
"io/ioutil"
"net/http"
"time"
)
//https://status.github.com/api
type Api struct {
StatusURL string `json:"status_url"`
MessagesURL string `json:"messages_url"`
LastMessageURL string `json:"last_message_url"`
DailySummary string `json:"daily_summary"`
}
type Status struct {
Status string `json:"status"`
LastUpdated time.Time `json:"last_updated"`
}
func Get() bool {
url := GetURL()
result := GetStatus(url)
return result
}
func GetStatus(url string) bool {
resp, _ := http.Get(url)
defer resp.Body.Close()
byt, _ := ioutil.ReadAll(resp.Body)
var dat Status
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
if dat.Status == "good" {
return true
} else {
return false
}
}
func GetURL() string {
url := "https://status.github.com/api.json"
resp, _ := http.Get(url)
defer resp.Body.Close()
byt, _ := ioutil.ReadAll(resp.Body)
var dat Api
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
return dat.StatusURL
}
package breaktime
import "testing"
func TestGet(t *testing.T) {
if !Get(){
t.Fail()
}
}
#import "ViewController.h"
#import "Breaktime/Breaktime.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *TextStatus;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)TouchDown:(id)sender {
NSLog(@"%@",GoBreaktimeGetURL());
if(!GoBreaktimeGet()){
[_TextStatus setText:@"beer! beer! beer!"];
}else{
[_TextStatus setText:@"work! work! work!"];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.