第一个golang程序 ( v1.0 v2.0 )

v2.0

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

package mainpackage main

import (
    "bufio"
    "flag"
    "fmt"
    "io"
    "log"
    "os"
    "os/exec"
    "path/filepath"
    "strings"
    "time"
)
const (
    filename = "c:\\myetc\\remote.etc"
)
func main() {
    var servicefilepath string
    var serviceIP string
    var passwd string
    var serviceusername string
    var port string
    //参数判断
    if len(os.Args) == 1 {
        fmt.Printf("usage: %s filename1 filename2 ...\n", filepath.Base(os.Args[0]))
        return
    }
    //判断文件是否存在
    _, err := os.Stat(filename)
    if err != nil {
        log.Print("文件不存在!")
        return
    }
    f, err := os.Open(filename) //打开文件
    //打开文件出错处理, 也可以是 defer f.Close(), 这里这样写是可以在里面添加内容
    defer func() {
        f.Close()
    }()
    if nil == err {
        buff := bufio.NewReader(f) //读入缓存
        for {
            line, err := buff.ReadString('\n') //以'\n'为结束符读入一行
            if err != nil || io.EOF == err {
                break
            }
            //如果是#开头的则认为是注释符
            strings.Trim(line, " ")
            if ok := strings.Contains(line, "#"); ok {
                continue
            }
            //去回车换行符
            //样例 m_ta/[email protected]:22:/home/m_ta/src
           // splitstrings := strings.Split(strings.Replace(line, "\r\n", "", -1), ":")
           //取值写是不是很好,以后再改进 
          //   servicefilepath = splitstrings[2]
          //  port = splitstrings[1]
          //  tmp := strings.Split(splitstrings[0], "@")
          //  serviceIP = tmp[1]
         //    tmp2 := strings.Split(tmp[0], "/")
         //   passwd = tmp2[0]
          // serviceusername = tmp2[1]

          //用以下方法来进行分割
         splitstrings := strings.FieldsFunc(strings.Replace(line, "\r\n", "", -1), func(char rune) bool {
                switch char {
                case ':', '/', '@':
                    return true
                }
                return false
            })
            passwd, serviceusername, serviceIP, port = splitstrings[0], splitstrings[1], splitstrings[2], splitstrings[3]            
            servicefilepath = "/" + strings.Join(splitstrings[4:], "/")

        }
        parameter := serviceusername + "@" + serviceIP + ":" + servicefilepath
        flag.Parse() // Scans the arg list and sets up flags
        for i := 0; i < flag.NArg(); i++ {
            cmd := exec.Command("pscp.exe", "-P", port, "-pw", passwd, flag.Arg(i), parameter)
            err := cmd.Run()
            if err != nil {
                log.Print(err)
            } else {
                log.Print("OK!")
            }
        }
        time.Sleep(1 * time.Second)
    }
}


v.10
=========================
import (
    "flag"
    "log"
    "os/exec"
    "strconv"
    "time"
)

func main() {
    port:=22
    passwd:="hstest"
    serviceusername:="hstest"
    serviceIP:="192.168.224.128"
    servicefilepath:="/home/hstest/src"
    parameter:= serviceusername + "@" + serviceIP + ":" + servicefilepath

    flag.Parse()   // Scans the arg list and sets up flags
    for i := 0; i < flag.NArg(); i++ {
        cmd:=exec.Command("pscp.exe", "-P", strconv.Itoa(port), "-pw", passwd, flag.Arg(i), parameter)
        err:=cmd.Run()
        if err != nil {
            log.Print(err)
        }else{
            log.Print("OK!")

        }

    }

time.Sleep(1*time.Second)

}

功能: 把文件上传到指定服务器, 在windows中,右键->打开, 可以直接上传指定文件,但不能上传多个文件,命令行可以

文件名为: Upfile.go

生成的执行文件为:Upfile.exe

用法为: Upfile  filename1 filename2


你可能感兴趣的:(golang)