這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)如何在golang中處理輸入,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
專注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)制作、網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)尉氏免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
golang處理輸入的方法:1、【fmt.Scan】交互式接受輸入,通過空格來分詞;2、【fmt.Scanln】要指定接收輸入的變量名和變量數(shù);3、【 fmt.Scanf】需要指定輸入的格式,直接把不需要的部分過濾掉。
golang處理輸入的方法:
1. fmt.Scan
fmt.Scan
交互式接受輸入,通過空格來分詞。調(diào)用Scan函數(shù)時(shí),要指定接收輸入的變量名和變量數(shù)。
直到接收完所有指定的變量數(shù),Scan函數(shù)才會(huì)返回,回車符也無法提前讓它返回。
fmt.Println("Please enter the firstName and secondName: ") fmt.Scan(&afirstName, &asecondName) fmt.Printf("firstName is %s, secondName is %s\n", afirstName, asecondName)
結(jié)果如下:
Please enter the firstName and secondName: zz rr firstName is zz, secondName is rr
2. fmt.Scanln
Scanln
調(diào)用時(shí),也要指定接收輸入的變量名和變量數(shù)。
它同Scan的區(qū)別,在于 \ n
會(huì)讓函數(shù)提前返回,將返回時(shí)還未接收到值的變量賦為空。
fmt.Println("Please enter the firstName and secondName: ") fmt.Scanln(&bfirstName, &bsecondName) fmt.Printf("firstName is %s, secondName is %s\n", bfirstName, bsecondName)
結(jié)果如下:
Please enter the firstName and secondName: zr firstName is zr, secondName is
3. fmt.Scanf
用Scanf
處理輸入,是比較靈活的一種處理方式。
需要指定輸入的格式,適用于完全了解輸入格式的場景,可以直接把不需要的部分過濾掉。
fmt.Println("Please enter the firstName and secondName: ") fmt.Scanf("//%s\n%s", &cfirstName, &csecondName) fmt.Printf("firstName is %s, secondName is %s", cfirstName, csecondName)
結(jié)果如下:
1)這個(gè)場景,在接收輸入時(shí),就把不需要的部分“//” 和 “\n”過濾掉了,接收到是有用的兩個(gè)字符串zz和rr。
Please enter the firstName and secondName: //zz rr firstName is zz, secondName is rr
2)如果輸入不符合指定的格式,從不符合處開始,其后的變量值都為空。
Please enter the firstName and secondName: //zr ui firstName is zr, secondName is
上述就是小編為大家分享的如何在golang中處理輸入了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
名稱欄目:如何在golang中處理輸入
網(wǎng)址分享:http://jinyejixie.com/article8/gpship.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、企業(yè)建站、外貿(mào)建站、動(dòng)態(tài)網(wǎng)站、域名注冊、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)