New to the language, need a bit of help on personal project

agolangf · · 432 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>So i&#39;m making a Blackjack program as a way to learn the language a bit better and i literally have had several iterations of this trying to find different ways i want to approach the project. I decided to exclusively work on the card section of the program which is in its (very) elementary stage. Anywho, can someone point me in the right direction? What am i doing wrong, why isnt this working etc.</p> <p>Code: // Blackjack package main</p> <pre><code>import ( &#34;fmt&#34; &#34;math/rand&#34; &#34;time&#34; ) type Card struct{ CardName string Value int } type Deck [] Card //create the deck func deckSetup () []Card{ deck := Deck{ {CardName: &#34;Jack&#34;, Value: 10}, {CardName:&#34;Queen&#34;, Value: 11}, {CardName:&#34;king&#34; ,Value: 10}, } return deck } //shuffles and draws the first card. Should also remove the card via removeCard() func shuffleAndDraws (setOfCards *Deck) (Card, []Card){ rand.Seed(time.Now().UnixNano()) i:=rand.Intn(3) newCard := setOfCards[i] setOfCards= removeCard(setOfCards,i) return newCard, setOfCards } //removes card and returns the entire deck without the card func removeCard(setOfCards *Deck, choiceElement int) []Card{ setOfCards = append(setOfCards[:choiceElement], setOfCards[choiceElement+1:]) return setOfCards } //will eventually remove main as i want this to be multiple files. Kept for testing func main() { newDeck := deckSetup() var yo Card yo, newDeck =shuffleAndDraws(&amp;newDeck) fmt.Println(newDeck) fmt.Println(yo) } </code></pre> <hr/> <p>Error(s):</p> <pre><code> # command-line-arguments ./Blackjack.go:32: cannot use removeCard(setOfCards, i) (type []Card) as type *Deck in assignment ./Blackjack.go:33: invalid operation: setOfCards[i] (type *Deck does not support indexing) ./Blackjack.go:33: cannot use setOfCards (type *Deck) as type []Card in return argument ./Blackjack.go:36: cannot slice setOfCards (type *Deck) ./Blackjack.go:37: cannot use setOfCards (type *Deck) as type []Card in return argument ./Blackjack.go:43: cannot use &amp;newDeck (type *[]Card) as type *Deck in argument to shuffleAndDraws Error: process exited with code 2. </code></pre> <p>Also, what exactly is the difference between &#34;*&#34; and &#34;&amp;&#34;?</p> <p>It also strikes me as odd there isnt a remove function for slices. Instead there just tricks to remove the desired index/element like what i did(which i found on goslicewiki in github). Why is this the case? Why not just incorporate that in the language?</p> <p>Also, i may have ambitions of creating a GUI for this if i ever finish it. Is there sufficient tools in go to create a gui program?</p> <p>I&#39;m not the most experienced programmer, in fact im still in school.</p> <hr/>**评论:**<br/><br/>pyThat: <pre><pre><code>package main import ( &#34;fmt&#34; &#34;math/rand&#34; &#34;time&#34; ) type Card struct { CardName string Value int } type Deck []Card //create the deck func deckSetup() []Card { deck := Deck{ {CardName: &#34;Jack&#34;, Value: 10}, {CardName: &#34;Queen&#34;, Value: 11}, {CardName: &#34;king&#34;, Value: 10}, } return deck } //shuffles and draws the first card. Should also remove the card via removeCard() func shuffleAndDraws(setOfCards Deck) (Card, []Card) { rand.Seed(time.Now().UnixNano()) i := rand.Intn(3) newCard := setOfCards[i] setOfCards = removeCard(setOfCards, i) return newCard, setOfCards } //removes card and returns the entire deck without the card func removeCard(setOfCards Deck, choiceElement int) []Card { return append(setOfCards[0:choiceElement], setOfCards[choiceElement+1:]...) } //will eventually remove main as i want this to be multiple files. Kept for testing func main() { newDeck := deckSetup() var yo Card yo, newDeck = shuffleAndDraws(newDeck) fmt.Println(newDeck) fmt.Println(yo) } </code></pre> <p>you don&#39;t need to use a pointer to a slice, I recommend you read about what a slice consist of.</p> <p>I did not read your code, I just fixed the errors to make it run.</p></pre>Prerogativ: <pre><p>my intention was that whatever changes (removing of a card) happens during parts of the program, it carries over rather than just arbitrarily drawing a card and the deck still has the original number of cards</p> <p>which was what i assumed the pointers would do</p> <p>EDIT: THANKS!! i still dont understand, isnt a slice just a copy of the original array (or portion of the array)?</p></pre>THUNDERGROOVE: <pre><p>No a slice is a dynamic array.</p> <p>Arrays and slices are always references not values in Go.</p></pre>GopherFromHell: <pre><p>arrays not are passed by reference, slices are. <a href="https://play.golang.org/p/GrFWjcP_SP" rel="nofollow">look here</a>. a slice is a pointer to an array with bounds checking <a href="http://blog.golang.org/go-slices-usage-and-internals" rel="nofollow">http://blog.golang.org/go-slices-usage-and-internals</a></p></pre>champioj: <pre><p>One reason for having no delete builtin for slice is because there is no single best way to remove an element. Try to find why there is a delete and a delete without preserving order in the slice trick page.</p></pre>Prerogativ: <pre><p>Thanks! Another question:</p> <p>Whats the difference between &#39;type&#39; and &#39;var&#39;. Why do i get an error when i do</p> <pre><code>var Deck []Card </code></pre> <p>but not</p> <pre><code>type Deck []Card </code></pre></pre>Prerogativ: <pre><p>So i&#39;m working on the player portion of the program and i&#39;m a bit stuck. Maps kind of allude me.</p> <pre><code>package main import( &#34;fmt&#34; ) type User struct{ Name string } type Player struct{ User score int } var Turn int type Players map[Player]turn func setPlayerInfo() map[Player]turn{ newPlayer := Players{ User{Name = &#34;josh&#34;}, 0 :0 } } func main(){ player1 := setPlayerInfo() fmt.Println(player1) } </code></pre> <hr/> <p>error:</p> <pre><code> # command-line-arguments ./Players.go:19: syntax error: unexpected =, expecting } Error: process exited with code 2. </code></pre> <p>Id someone can explain what i did wrong, thatd be great</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

432 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传