wuqingtao

第 4399 号会员 /

回复了 shuaishuai 创建的主题: golang的一道面试题

这个是不是可以定义一个以方向、x和y坐标的结构体,然后有前后左右四个方法

回复了 netaxcess 创建的主题: 开机无法自动运行golang程序

你需要使用启动脚本,就像这个样子,我删除了一部分,方法就是这样:

#!/bin/sh

### BEGIN INIT INFO
# Provides:          monitor
# Required-Start:    $remote_fs
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: monitor check the hosts online or not.
# Description:       monitor check every host 120s/times, sendmail if status changed.
### END INIT INFO

. /lib/lsb/init-functions

BASE="/usr/local/monitor"
HTTPPID="${BASE}/tmp/http.pid"
NAME="monitor"

do_status() {
    if test -f $1; then
        pid=`cat $1`
        comm=`ps --no-heading -p $pid -o comm`
        if test a$comm = a$NAME; then
            return 0
        else
            return 1
        fi
    fi
    return 10
}

case $1 in
    start)
        if do_status $HTTPPID; then
            log_warning_msg "$0 http already running"
            exit 0
        fi

        ${BASE}/$NAME http > /dev/null 2>&1 &
        sleep 3
        if do_status $HTTPPID; then
            log_success_msg "Start $NAME http"
            exit 0
        fi
        log_failure_msg "Start $NAME http"
        exit 1
        ;;
    stop)
        if do_status $HTTPPID; then
            pidh=`cat $HTTPPID`
            kill $pidh
            sleep 2
            commh=`ps --no-heading -p $pidh -o comm`
            if test x$commh = x -o y$commp = y; then
                rm -f $HTTPPID
                log_success_msg "Stop $NAME"
                exit 0
            else
                log_failure_msg "Stop $NAME"
                exit 1
            fi
        fi
        log_warning_msg "$NAME not running"
        exit 0
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    status)
        if do_status $HTTPPID; then
            log_success_msg "$NAME http is running: `cat $HTTPPID`"
        else
            log_warning_msg "$NAME http not running"
        fi
        ;;
    *)
        echo "Usage: $NAME (start|stop|restart|status)"
        exit 1
esac

回复了 jimmykuu 创建的主题: Go 排序代码悬赏

我也来一个

package main

import (
    "sort"
    "fmt"
    "strings"
)

var version = []string{
    "1.6beta1",
    "1.5rc1", 
    "1.5beta2",
    "1.5beta1", 
    "1.5.1", 
    "1.5", 
    "1.4rc2", 
    "1.4rc1", 
    "1.4beta1",
    "1.4.2", 
    "1.4.1",
    "1.4", 
    "1.3rc2",
    "1.3rc1", 
    "1.3beta2", 
    "1.3beta1", 
    "1.3.3", 
    "1.3.2", 
    "1.3.1", 
    "1.3", 
    "1.2rc5", 
    "1.2rc4", 
    "1.2rc3", 
    "1.2rc2", 
    "1.2rc1", 
    "1.2.2", 
    "1.2.1", 
    "1.2", 
    "1.1.2", 
    "1.1.1",
    "1.1", 
    "1.0.3",
    "1.0.2", 
    "1.5.2", 
    "1.5alpha1",
}

type Version []string

func (v Version) Len() int {
    return len(v)
}

func (v Version) Swap(i, j int) {
    v[i], v[j] = v[j], v[i]
}

func (v Version) Less(i, j int) bool {
    r := strings.NewReplacer("alpha","a","beta","b","rc","r","","z")
    return r.Replace(v[i]) < r.Replace(v[j])
}

func VersionSort(versions []string) []string {
    sort.Sort(Version(versions))
    return versions
}

func main() {
    fmt.Println(VersionSort(version))
}
D:\go\src\sortversion>sortversion.exe
[1.0.2 1.0.3 1.1 1.1.1 1.1.2 1.2rc1 1.2rc2 1.2rc3 1.2rc4 1.2rc5 1.2 1.2.1 1.2.2
1.3beta1 1.3beta2 1.3rc1 1.3rc2 1.3 1.3.1 1.3.2 1.3.3 1.4beta1 1.4rc1 1.4rc2 1.4
 1.4.1 1.4.2 1.5alpha1 1.5beta1 1.5beta2 1.5rc1 1.5 1.5.1 1.5.2 1.6beta1]

回复了 sky123 创建的主题: php VS nodejs VS golang

在同一台电脑上运行ab本来就测试不准吧,ab本身也很占资源