Convert map[string]interface{} to Struct as Generic Function in Go

Several Methods to Convert

There are several methods to convert map[string]interface{} to a struct in Go.

  • Using reflect. This is the fastest. But, it will produce complicated long code. If not well coded, it will have problems with nested map. I will not discuss this method.
  • Using json standard library. First we marshal the map as json string and after that unmarshal it as a struct. This is what I will be talking about. It is slow, but reliable. Nested map are handled performantly.
  • Using external library such as mitchellh/mapstructure. It is not maintained anymore. But, you can still use it, since it is stable and has achieved its original intention.
  • Using simple loop and check one by one using switch-case. You need to hard-coded all the map properties, hence it’s not practical since it can’t be reused for different struct type.

Converting to Struct

The code below is the original non-generic method that can be used to convert any map to string. Don’t forget to include the json tag as per your intention, so the json library can convert it correctly.

[Read More]
go 

Error Handling in Go

Error As Value

Error is treated as value in Go. No try/catch, but there is panic/recover. Other programming languages is using try/catch for regular error, while Go use panic/recover for fatal or unrecoverable condition such as nil pointer dereference and so on. Hence, panic/recover is not a direct replacement for try/catch.

For regular error, we need to return it like we return a variable. Some people are alright with this decision, but some are not because it makes the code verbose and you will find if err != nil are everywhere.

[Read More]
go 

Beberapa Tips Uji Tekan Beton

Pengantar

Apakah kalian sudah melakukan uji tekan beton dengan benar? Ada beberapa hal teknis terkait uji tekan beton yang kadang luput dari pengawasan kita. Beberapa hal di bawah ini semoga bisa menambah pengetahuan kita ketika hendak melakukan uji tekan beton, sesuai ASTM C39/C39M (Standard Test Method for Compressive Strength of Cylindrical Concrete Specimens).

Artikel ini merujuk pada ASTM C39/C39M 17B, namun versi terakhirnya adalah C39/C39M 21. Hal teknis lainnya yang lebih detail bisa merujuk langsung pada dokumen tersebut.

[Read More]

Menghitung Beban Dinamis pada Crane pada saat Lifting

Hukum Newton dan Perubahan Momentum

Bagaimana cara sederhana menghitung total beban crane akibat tambahan beban dinamis?

Mengacu pada Hukum Newton, gaya terjadi akibat adanya percepatan pada suatu massa. Namun, jika di-extend ke level selanjutnya, gaya terjadi akibat adanya perubahan momentum terhadap waktu.

$$ F = \dfrac{d(mv)}{dt} $$

$$ F = m\dfrac{dv}{dt} + v\dfrac{dm}{dt} $$

m = massa dalam kg dan v = kecepatan dalam m/s

Aplikasinya pada bidang struktur, beban statik dan dinamik hanya menggunakan bagian yang pertama saja (sisi kiri). Ini bisa kita artikan sebagai massa dengan perubahan kecepatan terhadap waktu.

[Read More]

Lokasi 1 Layer Tulangan Susut dan Rangkak pada Beton Slab on grade

Definisi

Slab on grade atau slab on ground adalah jenis struktur beton yang dipasang bersentuhan langsung di atas permukaan tanah. Disebut slab karena memiliki permukaan yang cukup luas. Pada umumnya slab on grade memiliki beban merata (uniform load) dan sejumlah beban titik (point load) jika nilainya cukup signifikan.

Slab on grade berbeda dengan pondasi dangkal yang pada umumnya memiliki kolom pendek atau pedestal di atasnya yang bisa dimodelkan sebagai beban titik.

[Read More]