go语言中的struct定义方式与C语言类似

type Human struct {
    Name string
    Age int
}

同时go语言还支持匿名字段(嵌入字段)的定义

type Student struct {
    Human
    Grade int
}

上述代码中的Human就是一个匿名字段,这个功能相当于其他语言中的继承
只要Human字段嵌入到了Student中,那么Student就默认包含了Human的所有字段
可以采用这种方式来赋值

student := new(Student)
student.Name = "小明"
student.Age = 18

也可以采用这样的方式

student := new(Student)
human := new(Human)
human.Name = "小明"
human.Age = 18
student.Human = human

闲来无事把周五商老板说的面试题解了一下
具体代码如下(Golang)

package main

import (
    "fmt"
)

const SIZE = 6
var a = [SIZE][SIZE] int {
    {2, 2, 3, 3, 5, 6},
    {2, 1, 3, 3, 4, 6},
    {2, 2, 2, 1, 1, 6},
    {1, 2, 4, 4, 5, 5},
    {2, 2, 2, 1, 1, 5},
    {1, 4, 4, 1, 3, 3},
}

var count int = 0 // 计数
var mark = [SIZE][SIZE] int {} // 用来标记该数字是否被遍历过

func main() {

    fmt.Println("原数据:", "\n")
    lenA := len(a); 
    for i := 0; i < lenA ; i++ {
        fmt.Println(a[i])
    }

    fmt.Println("\n计算结果:")

    for i := 0; i < lenA; i++ {
        lenX := len(a[i])
        for j := 0; j < lenX; j++ {
            if mark[i][j] == 0 {
                findSame(i, j, a[i][j])
                count ++
            }
        }
    }

    fmt.Printf("共%d个簇\n\n", count)

    fmt.Println("具体情况如下:\n")
    lenMark := len(mark)
    for i := 0; i < lenMark ; i++ {
        fmt.Println(mark[i])
    }
}

func findSame(i, j, x int) {

    tempI := i
    tempJ := j
    if mark[i][j] == 0 {

        mark[i][j] = count + 1
        // 往上找相同的数
        for {
            i--
            if i < 0 || a[i][j] != x {
                break
            }
            findSame(i, j, a[i][j])
        }
        // 往下
        i = tempI
        for {
            i++
            if i > SIZE - 1 || a[i][j] != x {
                break
            }
            findSame(i, j, a[i][j])
            
        }
        // 往左
        i = tempI
        for {
            j--
            if j < 0 || a[i][j] != x {
                break
            }
            findSame(i, j, a[i][j])
        }
        // 往右
        j = tempJ
        for {
            j++
            if j > SIZE - 1 || a[i][j] != x {
                break
            }
            findSame(i, j, a[i][j])
        }
    }
}

更新

经商老板指点,findSame中的for循环没必要。下面是改过之后的findSame

func findSame(i, j, x int) {

    if mark[i][j] == 0 {
        mark[i][j] = count + 1
        // 往上找相同的数
        if i-1 >= 0 && a[i-1][j] == x {
            findSame(i-1, j, a[i-1][j])
        }
        // 往下
        if i+1 < SIZE && a[i+1][j] == x {
            findSame(i+1, j, a[i+1][j])
        }
        // 往左
        if j-1 >= 0 && a[i][j-1] == x {
            findSame(i, j-1, a[i][j-1])
        }
        if j+1 < SIZE && a[i][j+1] == x {
            findSame(i, j+1, a[i][j+1])
        }
    }
}

对递归理解还是不够到位啊~不过现在应该OK了^_^

今天刷票,没想到公司ip被12306封了,看到鱼大的刷票软件支持socks5代理,于是想到用服务器搞个代理试试。
步骤如下

  1. 安装 apt-get install dante-server
  2. 创建日志文件夹 mkdir /var/log/sockd
  3. 修改配置文件 vi /etc/danted.conf 写入以下内容

    logoutput: /var/log/sockd/sockd.log
    internal: 服务器ip port = 1080
    external: 服务器ip
    method: username none
    user.privileged: proxyuser
    user.notprivileged: nobody
    user.libwrap: nobody
    client pass {
        from: 0.0.0.0/0 to: 0.0.0.0/0
        log: connect disconnect
    }
    pass {
        from: 0.0.0.0/0 to: 0.0.0.0/0 port gt 1023
        command: bind
        log: connect disconnect
    }
    pass {
        from: 0.0.0.0/0 to: 0.0.0.0/0
        command: connect udpassociate
        log: connect disconnect
    }
    block {
        from: 0.0.0.0/0 to: 0.0.0.0/0
        log: connect error
    }
  4. 启动 /etc/init.d/danted start
  5. 检测是否启动成功 netstat -anp | grep 1080

有个问题就是端口暴露出来 会被各种人扫到。。所以千万不能一直开着

梦到大学的时候了。。也不梦点有意思的,偏偏梦到要考试了,心里那个着急,啥都不会T_T,大学学得太差了!真是捉鸡。