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
47736d3d
Commit
47736d3d
authored
Mar 03, 2023
by
Muhammad, Hammad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
used http module
parent
771caf72
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
16 additions
and
76 deletions
+16
-76
go.mod
go.mod
+0
-3
main.go
main.go
+16
-25
snake_case_test.go
snake_case_test.go
+0
-48
No files found.
go.mod
deleted
100644 → 0
View file @
771caf72
module hello_world
go 1.20
main.go
View file @
47736d3d
...
...
@@ -2,36 +2,27 @@ package main
import
(
"fmt"
"strings"
"io"
"net/http"
"os"
)
func
main
()
{
str
:=
ToSnakeCase
(
"helloWorld !!!"
)
fmt
.
Println
(
str
)
}
func
ToSnakeCase
(
str
string
)
string
{
// step 0: handle empty string
if
str
==
""
{
return
""
}
type
logWriter
struct
{}
// step 1: CamelCase to snake_case
for
i
:=
0
;
i
<
len
(
str
);
i
++
{
if
str
[
i
]
>=
'A'
&&
str
[
i
]
<=
'Z'
{
if
i
==
0
{
str
=
strings
.
ToLower
(
string
(
str
[
i
]))
+
str
[
i
+
1
:
]
}
else
{
str
=
str
[
:
i
]
+
"_"
+
strings
.
ToLower
(
string
(
str
[
i
]))
+
str
[
i
+
1
:
]
}
}
func
main
()
{
resp
,
err
:=
http
.
Get
(
"http://google.com"
)
if
err
!=
nil
{
fmt
.
Println
(
"Error:"
,
err
)
os
.
Exit
(
1
)
}
// step 2: remove spaces
str
=
strings
.
ReplaceAll
(
str
,
" "
,
""
)
lw
:=
logWriter
{}
// step 3: remove double-underscores
str
=
strings
.
ReplaceAll
(
str
,
"__"
,
"_"
)
io
.
Copy
(
lw
,
resp
.
Body
)
}
return
str
func
(
logWriter
)
Write
(
bs
[]
byte
)
(
int
,
error
)
{
fmt
.
Println
(
string
(
bs
))
fmt
.
Println
(
"Just wrote this many bytes:"
,
len
(
bs
))
return
len
(
bs
),
nil
}
snake_case_test.go
deleted
100644 → 0
View file @
771caf72
package
main
import
"testing"
func
TestToSnakeCase
(
t
*
testing
.
T
)
{
type
testCase
struct
{
description
string
input
string
expected
string
}
testCases
:=
[]
testCase
{
{
description
:
"empty string"
,
input
:
""
,
expected
:
""
,
},
{
description
:
"single word"
,
input
:
"Hello"
,
expected
:
"hello"
,
},
{
description
:
"two words (camel case)"
,
input
:
"HelloWorld"
,
expected
:
"hello_world"
,
},
{
description
:
"two words with space"
,
input
:
"Hello World"
,
expected
:
"hello_world"
,
},
{
description
:
"two words with space and underscore"
,
input
:
"Hello_World"
,
expected
:
"hello_world"
,
},
}
for
_
,
tc
:=
range
testCases
{
t
.
Run
(
tc
.
description
,
func
(
t
*
testing
.
T
)
{
actual
:=
ToSnakeCase
(
tc
.
input
)
if
actual
!=
tc
.
expected
{
t
.
Errorf
(
"expected %s, got %s"
,
tc
.
expected
,
actual
)
}
})
}
}
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