Unicode标准在GO编程语言中的应用
来源:stackoverflow
时间:2024-03-06 18:09:23 440浏览 收藏
各位小伙伴们,大家好呀!看看今天我又给各位带来了什么文章?本文标题是《Unicode标准在GO编程语言中的应用》,很明显是关于Golang的文章哈哈哈,其中内容主要会涉及到等等,如果能帮到你,觉得很不错的话,欢迎各位多多点评和分享!
问题内容
我有一个字符串,其中包含通过 unicode 实现的从红心 a 到红心 10 的卡片(练习需要使用字符串,因此不需要数组或切片) 给定一个数字 n,我必须从该字符串中提取 n 张卡片。如果使用 for-range 得到的位数少于我需要的位数,我该怎么做?
package main
import (
"fmt"
"math/rand"
"strconv"
"strings"
"time"
)
func main() {
var n int
var deck string
rand.Seed(int64(time.Now().Nanosecond()))
n = readNumber()
deck = deckGenerator()
drawCards(deck, n)
}
func readNumber() (n int) {
for n <= 0 || n >= 10 {
fmt.Print("Enter number between 1 and 9: ")
fmt.Scan(&n)
}
return n
}
func deckGenerator() (deck string) {
for i := 0; i < 10; i++ {
deck += strconv.Itoa('\U0001F0B1' + i)
}
return deck
}
func drawCards(deck string, n int) {
for i := 0; i < n; i++ {
cardPulledOut, deck2 := drawCard(deck)
fmt.Println("Pulled out the card", cardPulledOut, "- Cards left in the deck:", deck2)
}
}
func drawCard(deck string) (cardPulledOut rune, deck2 string) {
for true {
card := rune(('\U0001F0B1') + rand.Intn(10)) //random number between 0 and 9 inclusive
for _, v := range deck {
fmt.Println(v, card)
/*
output: (infinity loop)
...
49 127156
53 127156
56 127156
...
*/
if v == card {
deck = strings.Replace(deck, string(card), "", 1)
return cardPulledOut, deck2
}
}
}
return
}正确答案
您的人口函数有一个错误:
func deckgenerator() (deck string) {
for i := 0; i < 10; i++ {
// deck += strconv.itoa('\u0001f0b1' + i) // converts integers to their string equivalent e.g. "127156"
deck += string(rune('\u0001f0b1' + i)) // fix
}
return deck
}
https://go.dev/play/p/E3sVRZK4exh
为需要的人提供正确的代码:
package main
import (
"fmt"
"math/rand"
"strings"
"time"
)
func main() {
var n int
var deck string
rand.Seed(int64(time.Now().Nanosecond()))
n = readNumber()
deck = deckGenerator()
drawCards(deck, n)
}
func readNumber() (n int) {
for n <= 0 || n >= 10 {
fmt.Print("Enter number between 1 and 9: ")
fmt.Scan(&n)
}
return n
}
func deckGenerator() (deck string) {
for i := 0; i < 10; i++ {
//deck += strconv.Itoa('\U0001F0B1' + i)
deck += string(rune('\U0001F0B1' + i))
}
return deck
}
func drawCards(deck string, n int) {
var cardPulledOut rune
for i := 0; i < n; i++ {
cardPulledOut, deck = drawCard(deck)
fmt.Println("Pulled out the card", string(rune(cardPulledOut)), "- Cards left in the deck:", deck)
}
}
func drawCard(deck string) (card rune, deck2 string) {
for true {
card = rune(('\U0001F0B1') + rand.Intn(10)) //random number between 0 and 9 inclusive
for _, v := range deck {
//fmt.Println(v, card)
if v == card {
deck2 = strings.Replace(deck, string(card), "", 1)
return card, deck2
}
}
}
return
}今天关于《Unicode标准在GO编程语言中的应用》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
502 收藏
-
Golang · Go问答 | 3天前 | go · 性能 · bufio · 日志处理 · 错误排查 · 分块读取 Go bufio.Scanner token too long Scanner.Buffer 大日志行501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
112 收藏
-
193 收藏
-
391 收藏
-
488 收藏
-
Golang · Go问答 | 1天前 | net/http · Go问答 · HTTP重试 · 请求体 · 接口稳定性 · net/http Go HTTP重试 Request.GetBody Request.Clone 请求体复用273 收藏
-
Golang · Go问答 | 1天前 | go · 安全 · net/http · HTTP重定向 · 请求头 · 请求头 Authorization 安全边界 CheckRedirect Go HTTP重定向268 收藏
-
Golang · Go问答 | 1天前 | 错误处理 · go · SQL · database/sql · 线上排查 · SCAN 查询结果 database/sql Go问答 rows.Next Rows.Err292 收藏
-
Golang · Go问答 | 1天前 | 超时 · 错误处理 · go · Context · errors.Is Go context deadline exceeded WithCancelCause309 收藏
-
385 收藏
-
286 收藏
-
182 收藏
-
Golang · Go问答 | 1天前 | 标准库 · bufio · 网络协议 · Go问答 · 流式读取 · peek Go bufio.Reader 协议解析 Go问答 Discard UnreadByte414 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习