Friday, August 24, 2012

用golang生成key不是大写的json数据

前一段时间用go写了一个查看perforce的history的小程序,go没有什么成熟的ui包,所以我选用web界面,程序本身做两件事情,一个后台更新perforce的history到本地的sqlite数据库,另外一个后台当作web服务器,处理client的request,生成json数据。

在写这个程序的时候,发现要生成json数据的结构里field名字首字母要大写。因为是一个自己用的小程序,也没有去深究到底可不可以生成首字母是小写的json数据。今天逛stackoverflow的时候看到有人问这个问题,看了看,原来go的json包有解决办法,只是自己看文档的时候没有怎么注意罢了。要加tag来做这件事情。

下面这段示例代码就的output:

{"field_a":1234,"field_b":"hello"}

package main

import "fmt"
import "encoding/json"

func main() {
    type Test struct {
        A int `json:"field_a"`
        B string `json:"field_b"`
    }
    A := Test{1234, "hello"}

    b, _ := json.Marshal(A)
    fmt.Println(string(b))
}

Thursday, August 23, 2012

个人第一个github项目

今天终于在github上有了一个真正的自己的项目,虽然是一个很简单,很小的项目,不过还是值得纪念一下。

项目地址: https://github.com/yongzhy/goploticus

这个是libploticus的Go语言封装。

ploticus的主页

A free, GPL, non-interactive software package for producing plots, charts, and graphics from data. It was developed in a Unix/C environment and runs onvarious Unix, Linux, and win32 systems. ploticus is good for automated or just-in-time graph generation, handles date and time data nicely, and has basic statistical capabilities. It allows significant user control over colors, styles, options and details. Ploticus is a mature package, available since 1999, and version 2.40 has more than 12,000 downloads to date. Ploticus was discussed in this 2008 review on Linux.com.

因为是第一次封装C的API,折腾了一阵子才发现要让cgo编译代码,import “C” 必须是单独一行.

可以编辑的代码:

  1. //#cgo LDFLAGS: -lploticus

  2. //#include <libploticus.h>

  3. //#include <stdlib.h>

  4. import "C"

不可以被编辑的代码:

  1. //#cgo LDFLAGS: -lploticus

  2. //#include <libploticus.h>

  3. //#include <stdlib.h>

  4. import (

  5. "C"

  6. )

什么东西,都要试了才真正知道。

Friday, August 10, 2012

TextMate开源了

Mac下面无比NB的文件编辑器TextMate开源了,代码已经放在github上了。
因为TextMate只支持Mac,曾经让我这个只有Linux和Windows的用户无比羡慕。在羡慕加嫉妒TextMate用户的时候,我发现了Sublime Text 2,这个也是一个强大的编辑器,很容易上手,也很容易定制。因为TextMate作者长时间没有更新,Sublime Text 2已经慢慢的蚕食了很多TextMate的市场。
现在TextMate开源了,希望有高手可以赶快移植到Linux,Windows上,我们选择编辑器的时候就又多了一个选择。

Wednesday, August 8, 2012

putty + tmux 结合实现命令行多窗口

最近要远程登录Linux系统做一些东西,用putty一次 只能够执行一个程序,要在不同的目录运行不同的东西,要打开几个putty,很是麻烦。Google了一下,找到tmux这个小程序,参考了别人的配置,自己稍加改动,现在使用起来真的很爽。

我的配置文件:

##########################################################
# STATUS BAR
set -g status-utf8 on
set -g status-key vi
set -g status-interval 1
set -g status-attr bright
set -g status-fg white
set -g status-bg black
set -g status-left-length 20
set -g status-left '#[fg=green][#[fg=red]#S#[fg=green]]#[default]'
set -g status-justify centre
set -g status-right '#[fg=green][%m/%d %H:%M:%S]#[default]'
setw -g window-status-current-format '#[fg=yellow](#I.#P#F#W)#[default]'
setw -g window-status-format '#I#F#W'


##########################################
## TERMINAL EMULATOR TITLES
set -g set-titles on
set -g set-titles-string "#(tmux ls | awk -F: '{print $1}' | xargs | sed 's/\ / | /g')"


##########################################
## KEy BINDINGS
unbind C-b
set -g prefix C-s

# - = vertical split
unbind '"'
bind - split-window -v

# | = horizontal split
unbind %
bind \ split-window -h

# VIM Style movement
bind k selectp -U   # choose top panel
bind j selectp -D   # choose bottom panel
bind h selectp -L   # choose left panel
bind l selectp -R   # choose right panel

# bind ALT+Arrow to select panel
bind -n M-Up selectp -U   # choose top panel
bind -n M-Down selectp -D   # choose bottom panel
bind -n M-Left selectp -L   # choose left panel
bind -n M-Right selectp -R   # choose right panel


# Keys for resize panel
bind -n F9 resizep -U 1   # up
bind -n F10 resizep -D 1  # down
bind -n F11 resizep -L 1  # left
bind -n F12 resizep -R 1  # right

##########################################
## BASIC CONFIG
## utf8 ability
setw -g utf8 on
#
## vi Style Editing
setw -g mode-keys vi
#
## Make mouse useful in copy mode
#setw -g mode-mouse on

#
## Allow mouse to select which pane to use
#set -g mouse-select-pane on
#
## Allow xterm titles in terminal window, terminal scrolling with scrollbar, and setting overrides of C-Up, C-Down, C-Left, C-Right
set -g terminal-overrides "xterm*:XT:smcup@:rmcup@:kUP5=\eOA:kDN5=\eOB:kLFT5=\eOD:kRIT5=\eOC"
#
## Scroll History
set -g history-limit 30000
#
## Set ability to capture on start and restore on exit window data when running an application
setw -g alternate-screen on
#
## Lower escape timing from 500ms to 50ms for quicker response to scroll-buffer access.
set -s escape-time 50