Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Golang
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Muhammad, Hammad
Golang
Commits
a853f759
Commit
a853f759
authored
Mar 03, 2023
by
Muhammad, Hammad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
created a separate file like a class and used in main function and write data on file
parent
e5041f69
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
76 additions
and
28 deletions
+76
-28
deck.go
deck.go
+71
-0
main.go
main.go
+5
-28
No files found.
deck.go
0 → 100644
View file @
a853f759
package
main
import
(
"fmt"
"io/ioutil"
"math/rand"
"os"
"strings"
"time"
)
// Create a new type of 'deck'
// which is a slice of strings
type
deck
[]
string
func
newDeck
()
deck
{
cards
:=
deck
{}
cardSuits
:=
[]
string
{
"Spades"
,
"Diamonds"
,
"Hearts"
,
"Clubs"
}
cardValues
:=
[]
string
{
"Ace"
,
"Two"
,
"Three"
,
"Four"
}
for
_
,
suit
:=
range
cardSuits
{
for
_
,
value
:=
range
cardValues
{
cards
=
append
(
cards
,
value
+
" of "
+
suit
)
}
}
return
cards
}
func
(
d
deck
)
print
()
{
for
i
,
card
:=
range
d
{
fmt
.
Println
(
i
,
card
)
}
}
func
deal
(
d
deck
,
handSize
int
)
(
deck
,
deck
)
{
return
d
[
:
handSize
],
d
[
handSize
:
]
}
func
(
d
deck
)
toString
()
string
{
return
strings
.
Join
([]
string
(
d
),
","
)
}
func
(
d
deck
)
saveToFile
(
filename
string
)
error
{
return
ioutil
.
WriteFile
(
filename
,
[]
byte
(
d
.
toString
()),
0666
)
}
func
newDeckFromFile
(
filename
string
)
deck
{
bs
,
err
:=
ioutil
.
ReadFile
(
filename
)
if
err
!=
nil
{
// Option #1 - log the error and return a call to newDeck()
// Option #2 - Log the error and entirely quit the program
fmt
.
Println
(
"Error:"
,
err
)
os
.
Exit
(
1
)
}
s
:=
strings
.
Split
(
string
(
bs
),
","
)
return
deck
(
s
)
}
func
(
d
deck
)
shuffle
()
{
source
:=
rand
.
NewSource
(
time
.
Now
()
.
UnixNano
())
r
:=
rand
.
New
(
source
)
for
i
:=
range
d
{
newPosition
:=
r
.
Intn
(
len
(
d
)
-
1
)
d
[
i
],
d
[
newPosition
]
=
d
[
newPosition
],
d
[
i
]
}
}
main.go
View file @
a853f759
package
main
package
main
import
"fmt"
type
ShapeCalculator
interface
{
Area
()
int
Perimeter
()
int
}
type
Rectangle
struct
{
Width
int
Height
int
}
func
(
r
*
Rectangle
)
Area
()
int
{
return
r
.
Width
*
r
.
Height
}
// func (r *Rectangle) Perimeter() int {
// return r.Width * r.Height
// }
// uncomment the following line to guarantee that Implementation implements all methods of SomeInterface
// var _ ShapeCalculator = (*Rectangle)(nil) // ← this is the line
func
main
()
{
func
main
()
{
rect
:=
&
Rectangle
{
cards
:=
newDeck
()
Width
:
10
,
cards
.
shuffle
()
Height
:
3
,
cards
.
print
()
}
fmt
.
Println
(
rect
.
Area
())
}
}
// Execute commond both file like : go run main.go deck.go
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment