首页 > 其他分享 >Go - scan

Go - scan

时间:2024-06-17 11:43:47浏览次数:6  
标签:name scan country fmt give Please Go your

 

func getUserInput() {
    fmt.Print("Please give me your name: ")
    var name string
    // Scan scans text read from standard input, storing successive space-separated values into successive arguments. 
    // Newlines count as space. It returns the number of items successfully scanned. 
    // If that is less than the number of arguments, err will report why.
    fmt.Scan(&name)
    fmt.Printf("Your name is %s.\n", name)

    fmt.Print("Please give me your country: ")
    var country string
    // Scanln is similar to Scan, but stops scanning at a newline and after the final item there must be a newline or EOF.
    fmt.Scanln(&country)
    fmt.Printf("Your country is %s.\n", country)
}

 

zzh@ZZHPC:/zdata/Github/mastering-go-expertise$ go run .
Please give me your name: Zhang Zhihui
Your name is Zhang.
Please give me your country: Your country is Zhihui.

 

func getUserInput() {
    fmt.Print("Please give me your name: ")
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan() // use `for scanner.Scan()` to keep reading
    line := scanner.Text()
    fmt.Printf("Your name is %s.\n", line)

    fmt.Print("Please give me your country: ")
    var country string
    fmt.Scanln(&country)
    fmt.Printf("Your country is %s.\n", country)
}

 

zzh@ZZHPC:/zdata/Github/mastering-go-expertise$ go run .
Please give me your name: Zhang Zhihui
Your name is Zhang Zhihui.
Please give me your country: China
Your country is China.

 

func getUserInput() {
    fmt.Print("Please give me your name: ")
    reader := bufio.NewReader(os.Stdin)
    line, err := reader.ReadString('\n')
    if err != nil {
        panic(err)
    }
    fmt.Printf("Your name is %s.\n", line)

    fmt.Print("Please give me your country: ")
    var country string
    fmt.Scanln(&country)
    fmt.Printf("Your country is %s.\n", country)
}

 

zzh@ZZHPC:/zdata/Github/mastering-go-expertise$ go run .
Please give me your name: Zhang Zhihui
Your name is Zhang Zhihui
.
Please give me your country: China
Your country is China.

 

标签:name,scan,country,fmt,give,Please,Go,your
From: https://www.cnblogs.com/zhangzhihui/p/18252076

相关文章

  • go操作duckdb
    什么是duckdbDuckDB是一个开源的嵌入式SQL数据库管理系统。与传统的SQL数据库不同,DuckDB不需要一个服务器进程,也不需要在系统上安装数据库软件。它被设计为轻量级和高性能,可以轻松嵌入到应用程序中,提供完整的SQL数据库功能。以下是DuckDB的一些关键特性:1.**轻量级**:DuckDB的......
  • wpscan 工具使用笔记
    安装参考:https://wpscan.com/how-to-install-wpscan/DockerWealsosupportDocker.Pulltherepowith:dockerpullwpscanteam/wpscanExampleDockercommandtoenumerateusernames:dockerrun-it--rmwpscanteam/wpscan--urlhttps://example.com/--enumerat......
  • go语言之异步并发
    1.首先讲一下匿名函数和闭包的概念,(仔细看,其实Python代码里很好看,只不过Go语言代码有点多,看起来很复杂的感觉)packagemainimport"fmt"funcmain(){/*注意:匿名函数和闭包往往是一起出现的*///不带参的匿名函数func(){fmt.Printf("不带参数的匿名......
  • Go 语言中值接收者和指针接收者方法调用的自动转换规则详解
    在Go语言中,方法的调用规则对于值接收者和指针接收者有一些特别的行为,这使得代码变得更加简洁和易用。我们来详细解释一下“方法值调用规则”和“方法表达式调用规则”,以及它们如何影响代码的行为。方法值调用(MethodValueCall)在Go中,方法调用的语法是receiver.method().当......
  • Mongodb UPDATE, 使用$position指定向数组中插入新元素的位置
    学习mongodb,体会mongodb的每一个使用细节,欢迎阅读威赞的文章。这是威赞发布的第72篇mongodb技术文章,欢迎浏览本专栏威赞发布的其他文章。如果您认为我的文章对您有帮助或者解决您的问题,欢迎在文章下面点个赞,或者关注威赞。谢谢。本篇文章,我们聊聊在mongodb数组更新的操作符$po......
  • PHP 程序员转 Go 语言的经历分享
    大家好,我是码农先森。之前有朋友让我分享从PHP转Go的经历,这次它来了。我主要从模仿、进阶、应用这三个方面来描述转Go的经历及心得。模仿是良好的开端,进阶是艰难的成长,应用是认知的提升。希望我的经历对大家能有所启发。模仿著名艺术家毕加索说过「模仿是人类一切学习的......
  • solidity签名机制和go联合调试学习
    1.solidity实现:1.1.引入eip712合约://SPDX-License-Identifier:MITpragmasolidity^0.8.0;/***@devhttps://eips.ethereum.org/EIPS/eip-712[EIP712]isastandardforhashingandsigningoftypedstructureddata.**TheencodingspecifiedintheEIPis......
  • Google Chrome Proxy error All In One
    GoogleChromeProxyerrorAllInOnemacOSWi-FiproxieswebproxyHTTPsecuritywebproxyHTTPSsocksproxydemos(......
  • c# 检测密码强度,评分规则仿google
    参考delphi的代码更改为C#Delphi检测密码强度规则(仿google)仿google评分规则一、密码长度:5分:小于等于4个字符10分:5到7字符25分:大于等于8个字符二、字母:0分:没有字母10分:全都是小(大)写字母20分:大小写混合字母三、数字:0分:没有数字10......
  • typora通过picgo配置图床
    满足大部分需求:gitee图床教程链接:https://zhuanlan.zhihu.com/p/567668223?utm_id=0但gitee有1M的图片限制,若需要高清图片,可配置腾讯云cos图床高清图片需求:腾讯云cos图床教程链接:https://cloud.tencent.com/developer/article/1834573两者优缺点对比:gitee腾讯云cos......