備忘録

備忘録

CordovaでカスタムURLスキームに対応する方法

Ⅰ. はじめに

タイトルの通り「CordovaでカスタムURLスキームに対応する方法」です。

Ⅱ. やり方

1. Cordova プラグインをインストールする
cordova plugin add cordova-plugin-customurlscheme --variable URL_SCHEME=myapp
2. プログラムを書く

src/main.js

window.handleOpenURL = (url) => {
  alert(`Cordova : handleOpenURL\n${url}`)
}

Ⅲ. URIスキームを変更する時

以下のコマンドを実行する

cordova platform rm android
cordova plugin rm cordova-plugin-customurlscheme
cordova platform add android

cordova plugin add cordova-plugin-customurlscheme --variable URL_SCHEME=myapp001

GoでHTMLの要素をXPathで取得する方法

Ⅰ. はじめに

タイトルの通り「GoでHTMLの要素をXPathで取得する方法」です。

Ⅱ. やり方

1. パッケージをインストールする
go get github.com/antchfx/htmlquery
2. サンプルプログラムを書く
package main

import (
  "fmt"

  "github.com/antchfx/htmlquery"
)

func main() {
  doc, _ := htmlquery.LoadURL("http://example.com")

  // h1タグのInnerTextを取得する
  titleElement := htmlquery.FindOne(doc, "/html/body/div/h1")
  fmt.Println(htmlquery.InnerText(titleElement))

  // aタグのhref属性を取得する
  linkElement := htmlquery.FindOne(doc, "/html/body/div/p[2]/a")
  fmt.Println(htmlquery.SelectAttr(linkElement, "href"))

  // pタグを列挙し、InnerTextを表示する
  for i, x := range htmlquery.Find(doc, "/html/body/div/p") {
    fmt.Println(i, htmlquery.InnerText(x))
  }
}
3. 実行結果
Example Domain
http://www.iana.org/domains/example
0 This domain is established to be used for illustrative examples in documents. You may use this
    domain in examples without prior coordination or asking for permission.
1 More information...

その他

http://example.com の HTML が変わるとサンプルの意味が無くなるので、
念のため http://example.com のバックアップを残します。
https://gist.github.com/anonymous/e325a74047edab47a2cf6ccdef60af95

Linuxに最新版のGoをインストールする方法

Ⅰ. はじめに

タイトルの通り「Ubuntuに最新版のGoをインストールする方法」です。

Ⅱ. やり方(Ubuntu

1. 以下のコマンドを実行する
add-apt-repository ppa:longsleep/golang-backports
apt-get update
apt-get install golang-go
2. 環境変数を設定する

~/.bash_profile

export GOPATH=~/go  
export PATH=$GOPATH/bin:$PATH
source ~/.bash_profile
3. Go を実行する
$ go version
go version go1.12.8 linux/amd64

Ⅲ. やり方(CentOS

1. 以下のコマンドを実行する

※非公式リポジトリです

rpm --import https://mirror.go-repo.io/centos/RPM-GPG-KEY-GO-REPO
curl -s https://mirror.go-repo.io/centos/go-repo.repo | tee /etc/yum.repos.d/go-repo.repo
yum install golang
2. 環境変数を設定する

~/.bash_profile

export GOPATH=~/go  
export PATH=$GOPATH/bin:$PATH
source ~/.bash_profile
3. Go を実行する
$ go version
go version go1.12.9 linux/amd64

GoでJSONをシリアライズ、デシリアライズする方法

Ⅰ. はじめに

タイトルの通り「GoでJSONシリアライズ、デシリアライズする方法」です。

Ⅱ. シリアライズ(型あり)

サンプルプログラム
package main

import (
  "encoding/json"
  "fmt"
)

type Book struct {
  Id         int      `json:"id"`
  Title      string   `json:"name"`
  Categories []string `json:"categories"`
}

func main() {
  book := new(Book)

  book.Id = 1
  book.Title = "title001"
  book.Categories = []string{"category001", "category002", "category003"}

  // jsonBytes, _ := json.Marshal(book)
  jsonBytes, _ := json.MarshalIndent(book, "", "  ")
  jsonStr := string(jsonBytes)

  fmt.Println(jsonStr)
}
実行結果
{
  "id": 1,
  "name": "title001",
  "categories": [
    "category001",
    "category002",
    "category003"
  ]
}

Ⅲ. シリアライズ(型なし)

package main

import (
  "encoding/json"
  "fmt"
)

func main() {
  book := map[string]interface{}{
    "id":         1,
    "title":      "title001",
    "categories": []string{"category001", "category002", "category003"},
  }

  // jsonBytes, _ := json.Marshal(book)
  jsonBytes, _ := json.MarshalIndent(book, "", "  ")
  jsonStr := string(jsonBytes)

  fmt.Println(jsonStr)
}
実行結果
省略

Ⅳ. デシリアライズ(型あり)

サンプルプログラム
package main

import (
  "encoding/json"
  "fmt"
)

type Book struct {
  Id         int      `json:"id"`
  Title      string   `json:"name"`
  Categories []string `json:"categories"`
}

func main() {
  str := "{\"id\":1,\"name\":\"title001\",\"categories\":[\"category001\",\"category002\",\"category003\"]}"

  data := Book{}
  json.Unmarshal([]byte(str), &data)
  fmt.Printf("%+v", data)
}
実行結果
{Id:1 Title:title001 Categories:[category001 category002 category003]}

Ⅴ. デシリアライズ(型なし)

サンプルプログラム
package main

import (
  "encoding/json"
  "fmt"
)

func main() {
  str := "{\"id\":1,\"name\":\"title001\",\"categories\":[\"category001\",\"category002\",\"category003\"]}"

  var data map[string]interface{}

  json.Unmarshal([]byte(str), &data)

  fmt.Println(data["id"])
  fmt.Println(data["categories"])
  fmt.Println(data["categories"].([]interface{})[0])
}
実行結果
1
[category001 category002 category003]
category001

Goでexeファイルビルド時に自動的にアイコンを設定する方法

Ⅰ. はじめに

タイトルの通り「Goでexeファイルビルド時に自動的にアイコンを設定する方法」です。

Ⅱ. やり方

1. rsrc をダウンロードする

https://github.com/akavel/rsrc/releases

2. rsrc を利用して syso ファイルを作成する
rsrc -ico icon.ico -o resource.syso
3. ビルドする
go build -o out.exe

実行結果

f:id:kagasu:20190828152929p:plain