├── hello-world.go ├── README.md ├── sumArray.go ├── goroutine_example.go ├── .github └── FUNDING.yml ├── json_tag_test.go ├── figure-calculator.go ├── json_encode_test.go └── golang_sql_example.go /hello-world.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println("Hello, World!") 7 | } 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Golang-Project 2 | Feel free to create new file, don't hesitate to pull your code, the most important thing is that the file name here must match your nickname so that file does not conflict with other people. 3 | -------------------------------------------------------------------------------- /sumArray.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var arr = [...]int{2, 3, 2, 4, 3} 7 | sum := 0 8 | for i := 0; i < len(arr); i++ { 9 | sum += arr[i] 10 | } 11 | fmt.Println(sum) 12 | } 13 | -------------------------------------------------------------------------------- /goroutine_example.go: -------------------------------------------------------------------------------- 1 | package code 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | "time" 7 | ) 8 | 9 | func SayHello() { 10 | fmt.Println("hello world") 11 | } 12 | 13 | func TestSayHello(t *testing.T) { 14 | go SayHello() 15 | fmt.Println("OKE") 16 | 17 | // if the block program is come to an end, but the thread still has 18 | // a gorountine, it will get killed automatically 19 | time.Sleep(1 * time.Second) 20 | } 21 | 22 | func TestEqualMap(t *testing.T) { 23 | // map1 := map[string]string{} 24 | map2 := make(map[string]string) 25 | 26 | fmt.Println(map2) 27 | } 28 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: kokonior 4 | patreon: kokonior 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry 13 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 14 | -------------------------------------------------------------------------------- /json_tag_test.go: -------------------------------------------------------------------------------- 1 | package json 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "testing" 7 | ) 8 | 9 | type Product struct { 10 | Id string `json:"id"` 11 | Name string `json:"name"` 12 | ImageUrl string `json:"imageUrl"` 13 | } 14 | 15 | func TestJsonTag(t *testing.T) { 16 | laptop := Product{ 17 | Id: "P-001", 18 | Name: "Macbook Pro 15", 19 | ImageUrl: "https://images/image.png", 20 | } 21 | 22 | // marhsall 23 | jsonBytes, err := json.Marshal(laptop) 24 | if err != nil { 25 | panic(err) 26 | } 27 | 28 | fmt.Println(string(jsonBytes)) 29 | 30 | // unmarshall 31 | jsonExample := `{"id":"P-001","name":"Macbook Pro 15","imageUrl":"https://images/image.png"}` 32 | bytesJson := []byte(jsonExample) 33 | 34 | product := new(Product) 35 | 36 | err = json.Unmarshal(bytesJson, product) 37 | if err != nil { 38 | panic(err) 39 | } 40 | 41 | fmt.Println(*product) 42 | 43 | } 44 | -------------------------------------------------------------------------------- /figure-calculator.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | var opt int 9 | 10 | fmt.Println("Figure Option : \n 1) Circle \n 2) Square \n 3) Triangle \n 4) Rectangle \n") 11 | fmt.Println("Insert your choice below : ") 12 | fmt.Scan(&opt) 13 | 14 | var area float32 15 | var circumference float32 16 | 17 | if(opt == 1){ 18 | var r float32 19 | var pi float32 20 | pi = 3.14 21 | 22 | fmt.Print("\n=== CIRCLE ===\n radius : ") 23 | fmt.Scan(&r) 24 | 25 | area = pi*r*r 26 | circumference = 2*pi*r 27 | } else if(opt == 2){ 28 | var s float32 29 | 30 | fmt.Print("\n=== SQUARE ===\n side : ") 31 | fmt.Scan(&s) 32 | 33 | area = s*s 34 | circumference = 4*s 35 | } else { 36 | fmt.Println("\nlooks like you choose the wrong option :((\n ") 37 | } 38 | 39 | fmt.Println("\n=== Result ===\n area : ", area, "\n circumference : ", circumference) 40 | 41 | } 42 | -------------------------------------------------------------------------------- /json_encode_test.go: -------------------------------------------------------------------------------- 1 | package json 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "testing" 7 | ) 8 | 9 | func TestEncodeJson(t *testing.T) { 10 | type Website struct { 11 | WebName string 12 | Link string 13 | } 14 | 15 | type Person struct { 16 | Name string 17 | Age int 18 | Married bool 19 | Hobby []string 20 | Website Website 21 | } 22 | 23 | neo := Person{ 24 | Name: "Neo Jarmawijaya", 25 | Age: 20, 26 | Married: false, 27 | Hobby: []string{ 28 | "Coding", 29 | "Gaming", 30 | "Reading", 31 | }, 32 | Website: Website{ 33 | WebName: "neo blog", 34 | Link: "https://neoj.com", 35 | }, 36 | } 37 | 38 | // marshall 39 | bytes, err := json.Marshal(neo) 40 | if err != nil { 41 | panic(err) 42 | } 43 | 44 | fmt.Println(string(bytes)) 45 | 46 | // unmarshall 47 | var obj Person 48 | err = json.Unmarshal(bytes, &obj) 49 | if err != nil { 50 | panic(err) 51 | } 52 | 53 | fmt.Println(obj) 54 | } 55 | -------------------------------------------------------------------------------- /golang_sql_example.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "database/sql" 6 | "fmt" 7 | "time" 8 | 9 | _ "github.com/go-sql-driver/mysql" 10 | ) 11 | 12 | func main() { 13 | // insertToDatabase() 14 | // queryDatabase() 15 | // SafeFromSqlInjection() 16 | // AutoIncrementQuery() 17 | // PrepareStmt() 18 | TransactionalDbOperation() 19 | } 20 | 21 | func TransactionalDbOperation() { 22 | db := getConnection() 23 | defer db.Close() 24 | 25 | ctx := context.Background() 26 | 27 | query := "INSERT INTO comments (email, comment) VALUES (?,?)" 28 | tx, err := db.Begin() 29 | 30 | if err != nil { 31 | panic(err) 32 | } 33 | 34 | for i := 10; i < 20; i++ { 35 | email := fmt.Sprintf("emailbaru%v@gmail.com", i) 36 | comment := fmt.Sprintf("komen ke-%v", i) 37 | 38 | _, err := tx.ExecContext(ctx, query, email, comment) 39 | 40 | if err != nil { 41 | panic(err) 42 | } 43 | } 44 | 45 | // you can either commit or rollback 46 | // tx.Commit() 47 | err = tx.Rollback() 48 | if err != nil { 49 | panic(err) 50 | } 51 | 52 | fmt.Println("BERHASIL") 53 | } 54 | 55 | // multiple insert 56 | func PrepareStmt() { 57 | db := getConnection() 58 | defer db.Close() 59 | 60 | ctx := context.Background() 61 | 62 | query := "INSERT INTO comments(email, comment) VALUES(?, ?)" 63 | stmt, err := db.PrepareContext(ctx, query) 64 | 65 | if err != nil { 66 | panic(err) 67 | } 68 | defer stmt.Close() 69 | 70 | for i := 0; i < 10; i++ { 71 | email := fmt.Sprintf("reno%v@gmail.com", i) 72 | comment := fmt.Sprintf("ini komentar ke-%v", i) 73 | 74 | _, err := stmt.ExecContext(ctx, email, comment) 75 | if err != nil { 76 | panic(err) 77 | } 78 | 79 | } 80 | 81 | fmt.Sprintln("SUKSES") 82 | } 83 | 84 | func AutoIncrementQuery() { 85 | db := getConnection() 86 | defer db.Close() 87 | 88 | ctx := context.Background() 89 | 90 | email := "reno@gmail.com" 91 | comment := "tes komentar" 92 | 93 | query := "INSERT INTO comments(email, comment) VALUES (?, ?)" 94 | result, err := db.ExecContext(ctx, query, email, comment) 95 | 96 | if err != nil { 97 | panic(err) 98 | } 99 | 100 | id, err := result.LastInsertId() 101 | if err != nil { 102 | panic(err) 103 | } 104 | 105 | fmt.Printf("sukses menambahkan data dengan id = %v\n", id) 106 | 107 | } 108 | 109 | func SafeFromSqlInjection() { 110 | db := getConnection() 111 | defer db.Close() 112 | 113 | ctx := context.Background() 114 | 115 | username := "admin" 116 | password := "admin" 117 | 118 | query := "SELECT username FROM user WHERE username = ? AND password = ?" 119 | rows, err := db.QueryContext(ctx, query, username, password) 120 | 121 | if err != nil { 122 | panic(err) 123 | } 124 | 125 | defer rows.Close() 126 | 127 | if rows.Next() { 128 | fmt.Println("berhasil") 129 | } else { 130 | fmt.Println("gagal") 131 | } 132 | 133 | } 134 | 135 | func queryDatabase() { 136 | db := getConnection() 137 | defer db.Close() 138 | 139 | context := context.Background() 140 | 141 | query := "SELECT * FROM customer" 142 | rows, err := db.QueryContext(context, query) 143 | 144 | if err != nil { 145 | panic(err) 146 | } 147 | defer rows.Close() 148 | 149 | // print data 150 | // while rows has next 151 | for rows.Next() { 152 | var id, name string 153 | 154 | err := rows.Scan(&id, &name) 155 | if err != nil { 156 | panic(err) 157 | } 158 | 159 | fmt.Println(id) 160 | fmt.Println(name) 161 | } 162 | 163 | } 164 | 165 | func insertToDatabase() { 166 | // id := "budi" 167 | // name := "BUDI" 168 | // query := fmt.Sprintf("INSERT INTO customer VALUES('%v', '%v')", id, name) 169 | // fmt.Println(query) 170 | 171 | db := getConnection() 172 | defer db.Close() 173 | 174 | // context 175 | context := context.Background() 176 | 177 | // exec context can be use either INSERT, UPDATE, or DELETE 178 | query := "INSERT INTO customer VALUES ('budi', 'budi')" 179 | _, err := db.ExecContext(context, query) 180 | 181 | if err != nil { 182 | panic(err) 183 | } 184 | 185 | fmt.Println("Berhasil tambah data") 186 | } 187 | 188 | func getConnection() *sql.DB { 189 | db, err := sql.Open("mysql", "root:@tcp(localhost:3306)/golang_db") 190 | 191 | if err != nil { 192 | fmt.Println(err) 193 | panic("Connection Failed") 194 | } 195 | 196 | // database pooling 197 | db.SetMaxIdleConns(10) 198 | db.SetMaxOpenConns(100) 199 | db.SetConnMaxIdleTime(time.Second * 5) 200 | db.SetConnMaxLifetime(time.Hour * 1) 201 | 202 | // defer db.Close() 203 | 204 | return db 205 | } 206 | --------------------------------------------------------------------------------