golang EX1.1.21

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang EX1.1.21相关的知识,希望对你有一定的参考价值。

def scan():
    while True:
        line = input()
        args = line.split(" ")
        if args[0] == "exit":
            break
        name ,a,b = args[0],int(args[1]),int(args[2])
        print(name,a,b,a/b)

if __name__ == '__main__':
    scan()
package com.test;

import java.util.Scanner;

public class Scan {
    public static void main(String[] args) {
        scan();
    }

    private static void scan() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            String line = sc.nextLine();
            String[] args = line.split(" ");
            String name = args[0];
            if (name.equals("exit")) {
                break;
            }
            int a = Integer.parseInt(args[1]);
            int b = Integer.parseInt(args[2]);
            System.out.printf("%s\t%d\t%d\t%.2f\n", name, a, b, (float) a / b);
        }
    }
}
package main

import "fmt"

func main() {
    Scan()
}
func Scan() {
    var name string
    var a, b int
    for {
        fmt.Scanf("%s %d %d", &name, &a, &b)
        if name == "exit" {
            break
        }
        fmt.Printf("%s\t%d\t%d\t%.3f\n", name, a, b, float32(a)/float32(b))
    }
}

golang 领取v2ex每日奖励。命令行参数1 =用户名,命令行参数2 =密码。%v2ex_dialy username pwd

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
	"regexp"
	"strings"
)

var loginUrl = "http://v2ex.com/signin"
var missionUrl = "http://v2ex.com/mission/daily"

var username = os.Args[1]
var password = os.Args[2]

type myCookieJar struct {
	cookies []*http.Cookie
}

func (c *myCookieJar) SetCookies(u *url.URL, cookies []*http.Cookie) {
	if c.cookies == nil {
		c.cookies = make([]*http.Cookie, 0)
	}

	for _, it := range cookies {
		c.cookies = append(c.cookies, it)
	}
}

func (c *myCookieJar) Cookies(u *url.URL) []*http.Cookie {
	return c.cookies
}

func main() {
	cookieJar := &myCookieJar{}
	client := http.Client{Jar: cookieJar}
	// login
	loginreq, _ := http.NewRequest("GET", loginUrl, nil)
	loginresp, _ := client.Do(loginreq)
	loginHtml, _ := ioutil.ReadAll(loginresp.Body)
	re := regexp.MustCompile(`<input type="hidden" value="(\d+)" name="once" />`)
	matched := re.FindStringSubmatch(string(loginHtml))
	params := url.Values{"next": {"/", "/"}, "u": {username}, "p": {password}, "once": {matched[1]}}
	req, _ := http.NewRequest("POST", loginUrl, strings.NewReader(params.Encode()))
	req.Header.Add("Host", "v2ex.com")
	req.Header.Add("Origin", "http://v2ex.com")
	req.Header.Add("Referer", loginUrl)
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	for _, c := range cookieJar.cookies {
		req.AddCookie(c)
	}
	client.Do(req)

	// try finish mission
	missionReq, _ := http.NewRequest("GET", missionUrl, nil)
	missionResp, _ := client.Do(missionReq)
	missionHtml, _ := ioutil.ReadAll(missionResp.Body)
	missionHtmlStr := string(missionHtml)
	if strings.Index(missionHtmlStr, "每日登录奖励已领取") > 0 {
		fmt.Println("每日登录奖励已领取")
	} else {
		re = regexp.MustCompile(`location.href = '(.*)'`)
		matched = re.FindStringSubmatch(missionHtmlStr)
		req, _ = http.NewRequest("GET", "http://www.v2ex.com"+matched[1], nil)
		client.Do(req)
	}
}

以上是关于golang EX1.1.21的主要内容,如果未能解决你的问题,请参考以下文章

Golang 入门

Golang入门到项目实战 第一个golang应用

golang编译androidso无法加载

golang如何打印内存内容

Golang入门到项目实战 golang匿名函数

json [Golang] golang #golang #snippets中有用的片段