-
Notifications
You must be signed in to change notification settings - Fork 94
Go Implementation #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Added Go Impl.
Create shortUrl.go
Go/shortUrl.go
Outdated
var s string | ||
for n > 0 { | ||
c := string(Alphabets[n%Base]) | ||
fmt.Println(c) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this println intended?
No, that was just for debugging.
Best Regards,
Sandeep Kalra
…On Tue, Jul 11, 2017 at 2:02 AM, Railsmechanic ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In Go/shortUrl.go
<#19 (comment)>:
> + "fmt"
+ "strings"
+)
+
+const (
+ Alphabets = "23456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ-_"
+ Base = len(Alphabets)
+)
+
+type Codec struct{}
+
+func (c *Codec) Encode(n int) string {
+ var s string
+ for n > 0 {
+ c := string(Alphabets[n%Base])
+ fmt.Println(c)
Is this println intended?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#19 (review)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AFTvwqmw4ZRAcNtI_dSS3g2qu8bp9bDGks5sMx4OgaJpZM4NsUCS>
.
|
Oh, totally forgot about this pull request. Sorry! Are the Thanks for your work, @sandeepkalra! |
Yes, the InitShorteningCodec is required. It returns the instance/object of the structure that has Encode and Decode functions. |
I coded as per GoLang paradigm . The library offers a instance of itself called " Codec{} " by using "InitShorteningCodec()" that has access functions "Encode()" and "Decode()". |
Encode function is not optimal, it allocates a new string for each step, you should use a bytes.buffer or a Strings.builder |
I wrote this before go ver1.10, and strings.builder wasn't there. I did try to play with it today after your comments, but the benchmark results didn't show anything promising for me to change. Please see ===== code ==== func (c *Codec) EncodeOpti(n int) string { func (c *Codec) Encode(n int) string { === benchmark === {Go}-> go test -bench=Encode2 (calling old code) |
I think my post earlier is a little too early. I have not studied strings.builder correctly I did another small change, and removed the sb.Grow(), and it seems to now run in 1/3rd time from previous. I will make changes to my code after I have full understanding of string.Builder library. |
Thanks, @bgadrian, and thanks for testing this and developing a modified implementation, @sandeepkalra! So this is now our third candidate for the Go implementation: #32 |
Go Impl.