stack.Push沒(méi)這個(gè)函數(shù)啊。Push是Stack這個(gè)結(jié)構(gòu)體的一個(gè)方法。你應(yīng)該寫(xiě)成s.Push。
https://segmentfault.com/q/10...
——————————————————————————————
第一次回復(fù)時(shí)沒(méi)看你的內(nèi)容,只回應(yīng)了標(biāo)題所以貼了個(gè)網(wǎng)址
下面正式回復(fù)
你的問(wèn)題不是cookies的問(wèn)題,我根據(jù)那篇文章改了下運(yùn)行成功了,你自己對(duì)照下
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"regexp"
"strings"
)
var cookies_lagou []*http.Cookie
const (
login_url_lagou string = "https://passport.lagou.com/login/login.html"
post_login_info_url_lagou string = "https://passport.lagou.com/login/login.json"
username_lagou string = "13330295142"
password_lagou string = "4525674692ac06e619cdb3f1b4b65b08"
)
func getToken(contents io.Reader) (string, string) {
data, _ := ioutil.ReadAll(contents)
regCode := regexp.MustCompile(`X_Anti_Forge_Code = '(.*?)';`)
if regCode == nil {
log.Fatal("解析Code出錯(cuò)...")
}
//提取關(guān)鍵信息
code := regCode.FindAllStringSubmatch(string(data), -1)[0][1]
regToken := regexp.MustCompile(`X_Anti_Forge_Token = '(.*?)';`)
if regToken == nil {
fmt.Println("MustCompile err")
}
//提取關(guān)鍵信息
token := regToken.FindAllStringSubmatch(string(data), -1)[0][1]
return token, code
}
func login_lagou() {
//獲取登陸界面的cookie
jar, _ := cookiejar.New(nil)
client := &http.Client{
Jar: jar,
}
req, _ := http.NewRequest("GET", login_url_lagou, nil)
res, _ := client.Do(req)
// for k, v := range res.Cookies() {
// fmt.Printf("%v=%v\n", k, v)
// }
token, code := getToken(res.Body)
fmt.Println(token, code)
//post數(shù)據(jù)
postValues := url.Values{}
postValues.Add("isValidate", "true")
postValues.Add("username", username_lagou)
postValues.Add("password", password_lagou)
postValues.Add("request_form_verifyCode", "")
postValues.Add("submit", "")
// body := ioutil.NopCloser(strings.NewReader(postValues.Encode())) //把form數(shù)據(jù)編下碼
// requ, _ := http.NewRequest("POST", post_login_info_url_lagou, nil)
requ, _ := http.NewRequest("POST", post_login_info_url_lagou, strings.NewReader(postValues.Encode()))
requ.Header.Set("Referer", "https://passport.lagou.com/login/login.html")
requ.Header.Set("X-Requested-With", "XMLHttpRequest")
requ.Header.Set("X-Anit-Forge-Token", token)
requ.Header.Set("X-Anit-Forge-Code", code)
requ.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:51.0) Gecko/20100101 Firefox/51.0")
requ.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
//for _, v := range res.Cookies() {
// requ.AddCookie(v)
//}
res, _ = client.Do(requ)
//cookies_lagou = res.Cookies()
data, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
fmt.Println(string(data))
}
func main() {
login_lagou()
}
運(yùn)行結(jié)果
15f131a3-74b0-4914-bd6e-2672f36675e1 28747049
{"content":{"rows":[]},"message":"該帳號(hào)不存在或密碼(驗(yàn)證碼)誤,請(qǐng)重新輸入","state":400,"submitCode":23207051,"submitToken":"666f51d4-ccef-462a-bb56-55cb97c5231a"}
Process finished with exit code 0可以看下這個(gè)源碼 http://bazaar.launchpad.net/+...:/session.go#L195
Dial 方法調(diào)用 DialWithTimeout 方法,然后又調(diào)用 DialWithInfo 方法,所以是有連接池的。 只是Dita方法為你做了方便方便的wrapper。
然后用 session.New() 或者 session.Copy() 獲取一個(gè)Diat返回的鏈接
已經(jīng)解決! 吐槽一下:審核真慢
嵌套結(jié)構(gòu)體:
type SongSingle struct {
Codes int `json:"code"`
Date struct {
Song struct { // 音樂(lè)
Count int `json:"count"`
Itemlist []struct {
Albummid string `json:"albummid"`
Docid string `json:"docid"`
Id string `json:"id"`
Mid string `json:"mid"`
Name string `json:"name"`
Singer string `json:"singer"`
} `json:"itemlist"`
Name string `json:"name"`
Order int `json:"order"`
Type int `json:"type"`
} `json:"song"`
Album struct { // 專(zhuān)輯
Count int `json:"count"`
Itemlist []struct {
docid string `json:"docid"`
Id string `json:"id"`
Mid string `json:"mid"`
Name string `json:"name"`
Pic string `json:"pic"`
Singer string `json:"singer"`
} `json:"itemlist"`
Name string `json:"name"`
Order int `json:"order"`
Type int `json:"type"`
} `json:"album"`
Singer struct { // 歌手
Count int `json:"count"`
Itemlist []struct {
docid string `json:"docid"`
Id string `json:"id"`
Mid string `json:"mid"`
Name string `json:"name"`
Pic string `json:"pic"`
Singer string `json:"singer"`
} `json:"itemlist"`
Name string `json:"name"`
Order int `json:"order"`
Type int `json:"type"`
} `json:"singer"`
Mv struct { // mv
Count int `json:"count"`
Itemlist []struct {
docid string `json:"docid"`
Id string `json:"id"`
Mid string `json:"mid"`
Name string `json:"name"`
Singer string `json:"singer"`
Vid string `json:"vid"`
} `json:"itemlist"`
Name string `json:"name"`
Order int `json:"order"`
Type int `json:"type"`
} `json:"mv"`
Name string `json:"name"`
Order int `json:"order"`
Type int `json:"type"`
} `json:"data"`
Subcode int `json:"subcode"`
}
遍歷結(jié)構(gòu)體取值:
Sbody := string(body)
var arrayData SongSingle
if err := json.Unmarshal([]byte(Sbody), &arrayData); err == nil {
// fmt.Println("碼:", arrayData.Subcode)
// fmt.Println("子碼:", arrayData.Subcode)
for _, Songitem := range arrayData.Date.Song.Itemlist {
fmt.Println("專(zhuān)輯ID:", Songitem.Albummid)
fmt.Println(" ID :", Songitem.Id)
fmt.Println("歌曲ID:", Songitem.Mid)
fmt.Println("歌 名:", Songitem.Name)
fmt.Println("歌 手:", Songitem.Singer)
fmt.Println("\n")
}
} else {
fmt.Println(err)
}
decimal帶兩位小數(shù),更精準(zhǔn)一些
你的 HASH 用法搞錯(cuò)了,請(qǐng)參考下面的代碼
/*
數(shù)字簽名示例代碼。
@author: 李毅
*/
package main
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha512"
"log"
)
type Demo struct {
PriKey *rsa.PrivateKey
}
func (demo *Demo) LoadPrivateKey() {
privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
log.Fatal(err)
}
demo.PriKey = privateKey
}
// 返回 digest, signature
func (demo *Demo) Sign(message string) ([]byte, []byte) {
messageBytes := bytes.NewBufferString(message)
hash := sha512.New()
hash.Write(messageBytes.Bytes())
digest := hash.Sum(nil)
signature, err := rsa.SignPKCS1v15(rand.Reader, demo.PriKey, crypto.SHA512, digest)
if err != nil {
log.Fatalf("rsa.SignPKCS1v15 error: %v\n", err)
}
return digest, signature
}
func (demo *Demo) Check(digest, signature []byte) bool {
err := rsa.VerifyPKCS1v15(&demo.PriKey.PublicKey, crypto.SHA512, digest, signature)
if err != nil {
log.Printf("rsa.VerifyPKCS1v15 error: %V\n", err)
return false
}
return true
}
func main() {
demo := &Demo{}
demo.LoadPrivateKey()
digest, signature := demo.Sign("hi")
if demo.Check(digest, signature) {
log.Printf("signature is good")
} else {
log.Printf("signature is bad")
}
}golang 的時(shí)間格式確實(shí)困擾新人,需要一點(diǎn)適應(yīng)時(shí)間。
這里只需要調(diào)用 time.Parse(),然后讀取 time.Unix() 結(jié)果便可。
時(shí)間格式應(yīng)該是 "2006-01-02T15:04:05.999-0700",請(qǐng)參考下面的示例
package main
import (
"fmt"
"testing"
"time"
)
func TestParseTime(t *testing.T) {
str := "2018-06-07T20:14:03.368+0800"
format := "2006-01-02T15:04:05.999-0700"
tm, err := time.Parse(format, str)
if err != nil {
t.Fatal(err)
}
_, zoffset := tm.Zone()
if tm.Year() != 2018 || tm.Month() != 6 || tm.Day() != 7 || tm.Hour() != 20 || tm.Minute() != 14 || tm.Second() != 3 || zoffset != 8*3600 {
t.Fatal(tm)
}
fmt.Println(tm.Unix())
}要將 a 轉(zhuǎn)換成接口 I,可以這樣 a.(I)。
redis 返回值類(lèi)型與 go 類(lèi)型關(guān)系
Redis type Go type
error redis.Error
integer int64
simple string string
bulk string []byte or nil if value not present.
array []interface{} or nil if value not present.
參考代碼
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
)
func main() {
c, _ := redis.Dial("tcp", ":6379")
var script = redis.NewScript(2, `
local rst={};
for i,v in pairs(KEYS) do
rst[i]=redis.call('get', v)
end;
return rst`)
reply, _ := script.Do(c, "key1", "key2")
array := reply.([]interface{})
for i, item := range array {
if item != nil {
// 假設(shè) key1,key2 均返回 bulk string
fmt.Println(i, string(item.([]byte)))
}
}
}那用交叉編譯啊,配置 OS 和 ARCH 這 2 個(gè)環(huán)境變量
已經(jīng)解決,解決方法,使用和linux本機(jī)相同的文件系統(tǒng),不能使用,win共享的文件夾,掛載到linux
從語(yǔ)法角度上講:傳值只是改變的副本,得傳指針才能修改。
select a.*,a.salary-b.salary as diff from salaries a left join salaries b on a.from_date=b.to_date where a.emp_no=10001 and b.emp_no=10001 order by from_date;
**行轉(zhuǎn)列很容易,自己百度下就行,但我覺(jué)得意義不大,你大可以在頁(yè)面展現(xiàn)的時(shí)候?qū)崿F(xiàn),而不是糾結(jié)于sql語(yǔ)句。**當(dāng)然是寫(xiě)一個(gè)新的結(jié)構(gòu)體啊,protoc生成的文件是不建議改的
。。。參見(jiàn)鏈接:https://blog.lab99.org/post/d...
type Serverslice2 struct {
Total int `json:"total"`
Count int `json:"count"`
Start int `json:"start"`
}
var s Serverslice2
str := `{"count": 10,"start": 0,"total": 1626}`
err := json.Unmarshal([]byte(str), &s)
fmt.Println(s, err)首先需要關(guān)閉其中一個(gè),你程序的外部要能找到你所需要關(guān)閉的協(xié)程,這個(gè)時(shí)候就需要注入一下標(biāo)識(shí)什么的,比如用map存儲(chǔ)這些標(biāo)識(shí),每一個(gè)協(xié)程注入一個(gè)channel,然后使用select在協(xié)程內(nèi)部運(yùn)行,判斷channel是否close掉,如果close掉,則退出協(xié)程~
正則版的來(lái)了:
"李某平".replace(/^(.+).(.)$/, "$1*$2");
"慕容某霜".replace(/^(.+).(.)$/, "$1*$2");依賴(lài),net依賴(lài)于net/http
北大青鳥(niǎo)APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專(zhuān)業(yè)的國(guó)家
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
北大課工場(chǎng)是北京大學(xué)校辦產(chǎn)業(yè)為響應(yīng)國(guó)家深化產(chǎn)教融合/校企合作的政策,積極推進(jìn)“中國(guó)制造2025”,實(shí)現(xiàn)中華民族偉大復(fù)興的升級(jí)產(chǎn)業(yè)鏈。利用北京大學(xué)優(yōu)質(zhì)教育資源及背
博為峰,中國(guó)職業(yè)人才培訓(xùn)領(lǐng)域的先行者
曾工作于聯(lián)想擔(dān)任系統(tǒng)開(kāi)發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_(kāi)發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開(kāi)發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開(kāi)發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開(kāi)發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。