Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
G
GoLang_Test
Project
Project
Details
Activity
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
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
rima anggraini
GoLang_Test
Commits
617ac106
Commit
617ac106
authored
Jul 08, 2019
by
rima anggraini
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
initial commit
parents
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
317 additions
and
0 deletions
+317
-0
Json.go
Json.go
+34
-0
WebAPI.go
WebAPI.go
+269
-0
mysql
github.com/go-sql-driver/mysql
+1
-0
mux
github.com/gorilla/mux
+1
-0
model.go
model.go
+12
-0
No files found.
Json.go
0 → 100644
View file @
617ac106
package
main
//modelnya
type
User
struct
{
FullName
string
`json:"Name"`
Age
int
}
// func main() {
// fmt.Println("---------DECODE JSON KE STRUCT-----------")
// var jsonString = `{"Name": "john wick", "Age": 27}`
// var jsonData = []byte(jsonString)
// var data User
// var err = json.Unmarshal(jsonData, &data)
// if err != nil {
// fmt.Println(err.Error())
// return
// }
// fmt.Println("user :", data.FullName)
// fmt.Println("age :", data.Age)
// fmt.Println("---------DECODE JSON KE INTERFACE-----------")
// var data1 map[string]interface{}
// json.Unmarshal(jsonData, &data1)
// fmt.Println("user :", data1["Name"])
// fmt.Println("age :", data1["Age"])
// }
WebAPI.go
0 → 100644
View file @
617ac106
package
main
import
(
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
_
"github.com/go-sql-driver/mysql"
)
type
student
struct
{
ID
string
Name
string
Grade
int
}
type
product
struct
{
ProductID
uint64
ProductName
string
Stock
int
}
type
Response
struct
{
Status
int
`json:"status"`
Message
string
`json:"message"`
Data
[]
product
}
type
productCategory
struct
{
ProductID
int
ProductName
string
Stock
int
}
type
category
struct
{
CategoryID
int
CategoryName
string
}
type
CategoryArr
struct
{
CategoryName
string
DataProduct
[]
productCategory
}
type
ResponseCategory
struct
{
Status
string
Message
string
data
CategoryArr
}
var
data
=
[]
student
{
student
{
"E001"
,
"ethan"
,
21
},
student
{
"W001"
,
"wick"
,
22
},
student
{
"B001"
,
"bourne"
,
23
},
student
{
"B002"
,
"bond"
,
23
},
}
func
users
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
if
r
.
Method
==
"POST"
{
var
result
,
err
=
json
.
Marshal
(
data
)
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusInternalServerError
)
return
}
w
.
Write
(
result
)
return
}
http
.
Error
(
w
,
""
,
http
.
StatusBadRequest
)
}
func
connect
()
(
*
sql
.
DB
,
error
)
{
db
,
err
:=
sql
.
Open
(
"mysql"
,
"root:@tcp(localhost:3306)/test"
)
if
err
!=
nil
{
return
nil
,
err
}
fmt
.
Println
(
db
)
return
db
,
nil
}
func
user
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
if
r
.
Method
==
"POST"
{
var
id
=
r
.
FormValue
(
"id"
)
var
result
[]
byte
var
err
error
for
_
,
each
:=
range
data
{
if
each
.
ID
==
id
{
result
,
err
=
json
.
Marshal
(
each
)
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusInternalServerError
)
return
}
w
.
Write
(
result
)
return
}
}
http
.
Error
(
w
,
"User not found"
,
http
.
StatusBadRequest
)
return
}
http
.
Error
(
w
,
""
,
http
.
StatusBadRequest
)
}
func
products
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
if
r
.
Method
==
"POST"
{
db
,
err
:=
connect
()
if
err
!=
nil
{
fmt
.
Println
(
err
.
Error
())
return
}
defer
db
.
Close
()
fmt
.
Println
(
"OK"
)
rows
,
err
:=
db
.
Query
(
"select ProductID, ProductName, Stock from Product"
)
if
err
!=
nil
{
fmt
.
Println
(
err
.
Error
())
return
}
defer
rows
.
Close
()
var
products
product
var
arr_product
[]
product
var
response
Response
if
err
!=
nil
{
log
.
Print
(
err
)
}
for
rows
.
Next
()
{
if
err
:=
rows
.
Scan
(
&
products
.
ProductID
,
&
products
.
ProductName
,
&
products
.
Stock
);
err
!=
nil
{
log
.
Fatal
(
err
.
Error
())
}
else
{
arr_product
=
append
(
arr_product
,
products
)
}
}
response
.
Status
=
1
response
.
Message
=
"Success"
response
.
Data
=
arr_product
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
json
.
NewEncoder
(
w
)
.
Encode
(
response
)
return
}
http
.
Error
(
w
,
""
,
http
.
StatusBadRequest
)
}
func
categoryfunc
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
if
r
.
Method
==
"POST"
{
db
,
err
:=
connect
()
if
err
!=
nil
{
fmt
.
Println
(
err
.
Error
())
return
}
defer
db
.
Close
()
fmt
.
Println
(
"OK"
)
var
productsresult
productCategory
var
arrayProduct
[]
productCategory
var
response
ResponseCategory
productCategory
,
err
:=
db
.
Query
(
"select ProductID, ProductName, Stock from Product WHERE CategoryID = 1"
)
fmt
.
Println
(
"-----------------"
)
if
err
!=
nil
{
log
.
Print
(
err
)
}
for
productCategory
.
Next
()
{
if
err
:=
productCategory
.
Scan
(
productsresult
.
ProductID
,
productsresult
.
ProductName
,
productsresult
.
Stock
);
err
!=
nil
{
log
.
Fatal
(
err
.
Error
())
}
else
{
arrayProduct
=
append
(
arrayProduct
,
productsresult
)
}
}
fmt
.
Println
(
"Masuk sini---------"
)
var
datacat
=
CategoryArr
{
CategoryName
:
"Makanan"
,
DataProduct
:
arrayProduct
,
}
response
.
Status
=
"OK"
response
.
Message
=
"Success"
response
.
data
=
datacat
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
json
.
NewEncoder
(
w
)
.
Encode
(
response
)
return
}
http
.
Error
(
w
,
""
,
http
.
StatusBadRequest
)
}
func
sqlQuery
()
{
db
,
err
:=
connect
()
if
err
!=
nil
{
fmt
.
Println
(
err
.
Error
())
return
}
defer
db
.
Close
()
fmt
.
Println
(
"OK"
)
rows
,
err
:=
db
.
Query
(
"select ProductID, ProductName, Stock from Product"
)
if
err
!=
nil
{
fmt
.
Println
(
err
.
Error
())
return
}
defer
rows
.
Close
()
var
result
[]
product
for
rows
.
Next
()
{
var
each
=
product
{}
var
err
=
rows
.
Scan
(
&
each
.
ProductID
,
&
each
.
ProductName
,
&
each
.
Stock
)
if
err
!=
nil
{
fmt
.
Println
(
err
.
Error
())
return
}
result
=
append
(
result
,
each
)
}
if
err
=
rows
.
Err
();
err
!=
nil
{
fmt
.
Println
(
err
.
Error
())
return
}
for
_
,
each
:=
range
result
{
fmt
.
Println
(
each
.
ProductName
)
}
}
func
main
()
{
http
.
HandleFunc
(
"/users"
,
users
)
http
.
HandleFunc
(
"/user"
,
user
)
http
.
HandleFunc
(
"/product"
,
products
)
http
.
HandleFunc
(
"/category"
,
categoryfunc
)
sqlQuery
()
fmt
.
Println
(
"starting web server at http://localhost:8080/"
)
http
.
ListenAndServe
(
":8080"
,
nil
)
}
mysql
@
877a9775
Subproject commit 877a9775f06853f611fb2d4e817d92479242d1cd
mux
@
d83b6ffe
Subproject commit d83b6ffe499a29cc05fc977988d0392851779620
model.go
0 → 100644
View file @
617ac106
package
main
type
Products
struct
{
Id
int64
`form:"id" json:"ProductId"`
ProductName
string
`form:"firstname" json:"Productname"`
Stock
int
`form:"lastname" json:"Stock"`
}
type
Response
struct
{
Status
int
`json:"status"`
Message
string
`json:"message"`
Data
[]
Products
}
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