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月16日 星期四

設定CentOS時間與NTP伺服器時間同步

將系統與NTP 的伺服器同步
設定步驟
1.以root 的身分登入
2. 執行下面的指令安裝NTP
# yum install ntp
3. 啟動服務
#chkconfig ntpd on
4. 設定系統和 time.stdtime.gov.tw 同步 (以time.stdtime.gov.tw為例)
#ntpdate time.stdtime.gov.tw
5.啟動ntp
# service ntpd start
6.可以執行下面的指令確定目前系統的時間
#date

Visual Studio 2017 建立 DLL(Dynamic Linking Library)

以 VS2017 為例

1.檔案 -> 新增 -> 專案



2.已安裝 -> Visual C++ -> Windows Desktop -> Windows 傳統式精靈



3. 應用程式類型 -> 動態連結程式庫(.dll) -> 空白專案




4. 在專案中加入 .cpp 與 .h 檔





5.在 (.h)檔 中加入以下程式碼

#pragma once
__declspec(dllexport) int myadd(int a, int b);

6.在 (.cpp) 檔 中加入以下程式碼

 #include "ProjectDll.h"
int myadd(int a, int b) {

    return a + b;
}

7. 建制 -> 重建方案 後即可產生 DLL 檔




呼叫剛剛建置的DLL

1.檔案 -> 新增 -> 專案 -> 已安裝 -> Visual C++ -> Windows Desktop -> Windows 傳統式精靈 -> 主控台應用程式



  
2.先在專案的屬性(property) -->  C/C++  -->  一般 (general)  -->其他Include目錄
 將剛剛編譯的DLL的(.h)檔的位置include進來

 
3.在連結器(linking) --> 一般(general)  --> 其他程式庫目錄
這邊將你剛剛編譯的DLL的(.lib)檔的路徑加進來

 





4.在連結器(linking) --> 輸入(input) -->其它相依性(additional dependencies)
輸入剛剛編譯的DLL(.lib)檔的名字



5.程式中include你的DLL的(.h)檔即可呼叫DLL中的方法來使用了

 #include "xxx.h"

6.compiler完,記得將你剛剛編譯的DLL檔放在你的EXE檔的同一個資料夾中








 

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年6月2日 星期五

調整 vim 設定

編輯 ~/.vimrc 這個檔案(若沒有請自行新增)

在檔案中加入所需要的設定

背景 (預設 set bg=light)
set bg=dark

只在 Normal 以及 Visual 模式使用滑鼠,也就是取消 Insert 模式的滑鼠
set mouse=nv


顯示行號
set number


搜尋不分大小寫
set ic

使用空白取代 Tab
set expandtab

自訂縮排 (Tab) 位元數

set tabstop=2
set shiftwidth=2


字數過長時換行
set wrap

不換行
set nowrap

高亮當前行 (水平)
set cursorline


捲動時保留底下 3 行
set scrolloff=3

儲存後退出即可


更多詳細介紹請看:
http://note.drx.tw/2008/01/vimrc-config.html

如要套用別人的 Color Scheme 可以參考
https://blog.longwin.com.tw/2009/03/choose-vim-color-scheme-2009/

2017年5月12日 星期五

[linux] rm 誤刪檔案

午覺剛睡醒,不小心手殘 rm 砍錯檔!!!! 特別紀錄一下補救方式

首先是安裝 extundelete 程式 ,下載回來之後解壓縮安裝
# tar xvf extundelete-0.2.4.tar.bz2
# cd extundelete-0.2.4
# ./configure
# make install

安裝好後在 src/ 資料夾底下可以看到執行檔
# cd src

查詢被刪除的檔案在哪一個檔案系統
# df -aT

救回特定目錄下的檔案(我所使用的選項)
# ./extundelete /dev/mapper/root --restore-directory /source_code

救回檔案系統下的全部檔案
# ./extundelete /dev/mapper/root --restore-all

救回特定檔案
# ./extundelete /dev/mapper/root --restore-file /source_code/test.txt

註:
/dev/mapper/root <- 檔案所在的檔案系統

/source_code <- 檔案所在的目錄

希望可以幫到大家

2017年2月22日 星期三

tee and grep command

隨手做筆記

時空背景:
在執行程式時,終端機上的訊息太多又跑太快,來不及看。(on CentOS)

Solution:
./exe | tee log
grep -rn Error log

在執行 "exe" 這個程式時透過 "|" pipeline 與 "tee" 這個 tool 將訊息同時顯示在終端機上並寫入 "log" 這個檔案

程式中斷後,透過 "grep" 這個 tool 在 "log" 這個檔案當中搜尋 "Error" 這關鍵字並顯示在終端機上

MAC
ref: https://unix.stackexchange.com/questions/145651/using-exec-and-tee-to-redirect-logs-to-stdout-and-a-log-file-in-the-same-time
./exe &> >(tee -a "$log_file")