顯示具有 GO language 標籤的文章。 顯示所有文章
顯示具有 GO language 標籤的文章。 顯示所有文章

2017年12月28日 星期四

install vim-go(on Mac OSX)

install vim-go 的插件
# curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
# git clone https://github.com/fatih/vim-go.git ~/.vim/plugged/vim-go

編輯 .vimrc
# vim ~/.vimrc

在 vimrc 中加入下列三行後儲存離開
call plug#begin()
Plug 'fatih/vim-go', { 'do': ':GoInstallBinaries' }
call plug#end()
 
最後再用 vim 去看 golang 的 code 就可以發現已經安裝好了
 
note: 注意 vim 的版本,若安裝失敗有可能是 vim 版本太舊請先更新
      # brew upgrade vim
 
 
使用方式:
 
執行,直接在 vim 底下輸入“:GoRun“ 即可看到結果.
 
編譯,在 vim 底下輸入“:GoBuild”,若成功可以看到 “vim-go: [build] SUCCESS”
  



reference:

https://github.com/fatih/vim-go-tutorial

http://www.evanlin.com/switch-ide-to-vim/

http://fealonelei.github.io/2015/08/18/MAC-Vim-IDE-for-Go(lang).html

2017年12月13日 星期三

golang mkdir permission error

今天在Linux底下執行golang的os.Mkdir發生資料夾權限問題,透過ls -al去看才發現資料夾的權限跟我在os.Mkdir所設定的權限完全不一樣.後來發現建立資料夾時除了需要指定權限外,也需要加上系統的Umask.

原code:
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. func main() {
  7. err := os.MkdirAll("gotest/", 0777)
  8. if err != nil {
  9. panic(err)
  10. }
  11. } 
 
修正後:
  1. package main

  2. import (
  3. "fmt"
  4. "os"
  5. "syscall"
  6. )

  7. func main() {
  8.     mask := syscall.Umask(0)
  9.     defer syscall.Umask(mask)
  10.     err := os.MkdirAll("gotest/", 0777)
  11.     if err != nil {
  12.         panic(err)
  13.     }
  14. }
 

2017年11月1日 星期三

GUI applications entirely in Golang(not finish)

golang GUI,隨手筆記

稍微做了點資料搜集,下方列出三種目前找到的第三方套件,目前看來QT對於各個平台支援較為齊全.
但也有人主推HTML5,沒有跨平台的問題.



QT:
https://github.com/therecipe/qt#linux-1

支援平台
Target                       Arch.                   Host OS
----------------------------------------------------------
windows.                  (32 / 64)              Any
android(+Wear)        arm                      Any
Linux                        64                        Any
macOS                      64                        macOS
iOS                           (arm + arm64)     macOS
Raspberry Pi             arm                      Any

Awesome:
https://awesome-go.com

QML:
https://github.com/go-qml/qml

HTML5:
透過http, websocket 等方式跟go溝通

2017年1月23日 星期一

golang cross-compiler on windows to...

Golang 本身就有提供cross-compile相關的工具

以下紀錄的是在windows x64下將golang cross-compiler 成 linux x86

1.先到 $GOROOT 底下的 src(我個人是 C:/go/src)建立一個build.bat檔

2.edit build.bat
set CGO_ENABLED=0
set GOROOT_BOOTSTRAP=C:/Go
:::::::x86:::::::
set GOARCH=386

set GOOS=windows
call make.bat --no-clean

set GOOS=linux
call make.bat --no-clean
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:::::::x64:::::::
set GOARCH=amd64

set GOOS=linux
call make.bat --no-clean

set GOOS=windows
call make.bat --no-clean
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

set GOARCH=amd64
set GOOS=windows
go get github.com/nsf/gocode
pause

3.執行 build.bat

4.到 $GOROOT/bin 檢查是否有安裝相關檔案(ex:linux_386)

5.到要編譯程式的資料夾底下,我將指令寫成cross-compiler2linux_x86.bat檔
cd %~dp0
set GOOS=linux
set GOARCH=386
go build helloworld.go
pause

6.執行cross-compiler2linuxx86.bat 就會生成 "helloworld"檔案

7.將 "helloworld" 檔案複製到linux x86底下執行確認。

=======================================================================

本篇cross-compile工具只有設定windows x86, x64,  linux x86,  x64四種,其他平台請參考下列原文敘述進行修改。

$GOOS and $GOARCH
The name of the target operating system and compilation architecture. These default to the values of $GOHOSTOS and $GOHOSTARCH respectively (described below).
Choices for $GOOS are darwin (Mac OS X 10.7 and above and iOS), dragonflyfreebsdlinuxnetbsdopenbsdplan9solaris and windows. Choices for $GOARCH are amd64 (64-bit x86, the most mature port), 386(32-bit x86), arm (32-bit ARM), arm64 (64-bit ARM), ppc64le (PowerPC 64-bit, little-endian), ppc64 (PowerPC 64-bit, big-endian), mips64le (MIPS 64-bit, little-endian), and mips64 (MIPS 64-bit, big-endian). The valid combinations of $GOOS and $GOARCH are:
$GOOS$GOARCH
androidarm
darwin386
darwinamd64
darwinarm
darwinarm64
dragonflyamd64
freebsd386
freebsdamd64
freebsdarm
linux386
linuxamd64
linuxarm
linuxarm64
linuxppc64
linuxppc64le
linuxmips64
linuxmips64le
netbsd386
netbsdamd64
netbsdarm
openbsd386
openbsdamd64
openbsdarm
plan9386
plan9amd64
solarisamd64
windows386
windowsamd64


ref:

2013年6月4日 星期二

GO lang學習筆記(12)

package main

import (
    "fmt"
    "math/cmplx"
)

var (
    ToBe   bool       = false
    MaxInt uint64     = 1<<64 - 1
    z      complex128 = cmplx.Sqrt(-5 + 12i)
)

func main() {
    const f = "%T(%v)\n"
    fmt.Printf(f, ToBe, ToBe)
    fmt.Printf(f, MaxInt, MaxInt)
    fmt.Printf(f, z, z)
}

將常數 f 定義為 %T(%v)
常數使用 const 關鍵字,常數值可以為 字元、字串、布林、數字 類型。

%T 為顯示 type (EX:int, float)
%v 為顯示 其值

GO lang學習筆記(9)

package main

import "fmt"

func swap(x, y string) (string, string) {
    return y, x
}

func c(x, y int)(sx,sy int){
    sx = y*x
    sy = -y*x
return 
}
func main() {
    a, b := swap("hello", "world")
    fmt.Println(a, b)
 
    fmt.Println(c(1, 2))
}

當沒有 return 值時,會將 function 定義 (sx,sy int) 以 string 形式 return

用 fmt.Println(c(1, 2)) 印出

GO lang 學習筆記 (8)

package main

import "fmt"

func swap(x, y string) (string, string) {
    return y, x
}

func c(x, y int) (int,int){ //注意return 個數定義
return y*x, -y*x
}

func main() {
    a, b := swap("hello", "world")
    fmt.Println(a, b)
    x, y := c(1, 2)
   
    fmt.Printf("%d,%d",x,y)
}