DEV Community

Cover image for Go สำหรับสร้าง Mobile Apps
Atthaphon Urairat
Atthaphon Urairat

Posted on • Edited on

3 1

Go สำหรับสร้าง Mobile Apps

Go นับเป็นภาษาใหม่ที่น่าจับตามอง อีกทั้งยังเป็นที่สนใจของหลายๆคน
โดยนอกเหนือจากความโดดเด่นในการสร้างเว็บไซต์ที่มีความเร็วในการประมวลผล
และรองรับการใช้งานพร้อมๆกันได้ดีกว่าภาษาอื่นๆ
อย่างเห็นได้ชัด ซื่งเราสามารถเห็นได้จากผลการทดสอบเปรียบเทียบในบทความต่างๆที่แพร่หลายอยู่ในขณะนี้

ในตอนนี้ Go ก็ได้มีความพยายามพัฒนา Project
ที่จะนำมาพัฒนาโปรแกรมมือถือซึ่งในขณะนี้ก็ถือว่ายังอยู่ในขั้นตอนของการพัฒนา
ซึ่งการนำตัวโปรเจ็คมาใช้ก็อาจมีความเสี่ยงและพบปัญหาการใช้งานบ้าง
ด้วยความที่ตัวโปรเจ็คกำลังอยู่ในขั้นตอนของการพัฒนา และยังไม่มีการซัพพอร์ตอย่างเป็นทางการ
จากทั้งทาง Google และ Go team เพราะตอนนี้ Go team
ก็กำลังพยายามเป็นอย่างมากในการปรับปรุงตัวโปรเจ็ค เราสามารถเข้าไปดูได้จาก github.com/golang/mobile

แต่สำหรับคนที่อยากลองของใหม่ก่อนใครเพื่อน ก็คงไม่ใช่ปัญหาใช่ไหมล่ะครับ
ถ้าคุณเป็นอีกคนหนึ่งที่เชื่อว่า "มาก่อนได้ก่อน"
อย่ารอให้มันเป็นที่นิยมก่อนแล้วค่อยกระโดดเข้ามาเลยครับ

Go Mobile

Go mobile เป็น repository ย่อย ของ Go project ที่รองรับระบบการทำงานของ Smart
phone ทั้ง Android และ iOS และมีเครื่องมือช่วยสำหรับการ build โปรแกรมของเรา

ในส่วนของการใช้งาน Go สำหรับสร้างแอพพลิเคชั่น นั้นสามารถแบ่งออกได้เป็น 2 แบบคือ

  1. ใช้ Go ในการสร้าง แอพพลิเคชั่น หรือให้ Golang เป็น Native application
  2. ใช้ Go ร่วมกับ Native application หรือ SDK applications and generating bindings

เลือกใช้ Go เป็น Native หรือ ใช้ Go เป็น Package เสริมดี

ในส่วนนี้ขึ้นอยู่กับลักษณะของโปรเจ็คที่เรากำลังทำ หรือกำลังจะทำ
เราลองมาดูข้อดีข้อเสียหรือข้อจำกัดของทั้งสองแบบนี้กันดีกว่า

ใช้ Go ในการสร้าง แอพพลิเคชั่น หรือให้ Golang เป็น Native application

ในส่วนของการที่เราจะใช้ Go เป็น Native นั้นหมายถีงการที่เราเลือกจะใช้ภาษา Go
เป็นภาษาหลักในการสร้าง แอพพลิเคชั่น ซึ่ง ณ ตอนนี้ (29 Dec 2018) Go mobile ยังมี
package ให้เราได้ใช้สำหรับสร้าง mobile แอพพลิเคชั่นไม่มากนักซึ่งก็จะมี

  • App control and configuration
  • OpenGL ES 2 bindings
  • Asset management
  • Event management
  • Experimental packages include OpenAL bindings, audio, font, sprite and motion sensors

เราสามารถเข้าไปดูตัวอย่างสำหรับ Go native application โค้ดได้ที่ golang.org/x/mobile/example

ใช้ Go ร่วมกับ Native application หรือ SDK applications and generating bindings

ข้อดีของการใช้ Go ร่วมกับ Native application

  • เราสามารถนำ Go package มาใช้ซ้ำได้โดยการเพิ่ม หรือ เสริมเข้าไปในตัวโปรเจ็คที่มีอยู่แล้ว โดยที่เราไม่ต้องแก้ไขตัวโปรเจ็คที่มีอยู่แล้ว
  • ตัว Go package เสริมที่เราสร้างไว้สามารถนำไปใช้กับ Native application ไม่ว่าจะเป็น Android หรือ iOS ก็ได้

ต่อไปนี้คือข้อจำกัดที่มี

  • ตอนนี้สามารถรองรับ types ได้เพียงไม่กี่ชนิดเท่านั้น ดูได้จาก subset of Go types
  • การใช้ language binding จะมีปัญหาในเรื่องของการประมวลผล
  • การเรียกใช้งาน APIs นั้นมีข้อจำกัด ดังนั้นเราจำเป็นต้องศีกษาข้อจำกัดของตัวภาษาที่เราจะทำการใช้งานด้วย

ติดตั้งและทดลองรันโปรแกรม

เรารู้แล้วว่าการใช้ Go ในการสร้าง Mobile application มีอยู่ด้วยกัน 2
แบบหลักๆ ซึ่งในบทความนี้เราจะทำการติดตั้งและทดลองรันโปรแกรม ในแบบที่ใช้ Go เป็น
Native application โดยจะทำการติดตั้งด้วยเครื่องมือที่ Go Mobile มีมาให้ และดาวน์โหลด
Source code ตัวอย่างมาติดตั้งเพื่อทำการ build APK สำหรับ Android
สำหรับทดสอบในมือถือกัน

ถ้าเพื่อนๆต้องการดูตัวอย่างการติดตั้งในแบบอื่นๆ ก็สามารถเข้าไปดูได้ที่ Go Mobile

สำหรับการใช้งาน Go mobile เราต้องทำการติดตั้ง Go 1.5 หรือเวอร์ชั่นที่สูงกว่า หรือย่างน้อยที่สุดต้องใช้ เวอร์ชั่น 1.7.4 ในกรณีที่คุณใช้ macOS sierra

ขั้นแรกเราต้องทำการติดตั้ง tools กันก่อน โดยการรันคำสั้งต่อไปนี้ใน terminal หรือ iTerm
ของคุณ

$ go get golang.org/x/mobile/cmd/gomobile
$ gomobile init # ใช้เวลาในการติดตั้งพอควร

หลังจากเราทำการติดตั้ง gomobile เสร็จ
เราก็ทำการดาวน์โหลดตัวอย่างมาไว้ในเครื่องของเราด้วยคำสั่ง

$ go get -d golang.org/x/mobile/example/basic

ต่อไปเราก็จะทำการ build ตัวโปรเจ็คที่เราเพิ่งดาวน์โหลดมาเพื่อสร้างไฟล์ APK
เพื่อที่จะทำการติดตั้งและทดสอบใน Smart phone ของเรา

$ gomobile build -target=android golang.org/x/mobile/example/basic

ในกรณีที่เกิด error gomobile: no Android NDK path is set. Please run gomobile init with the ndk-bundle installed through the Android SDK manager or with the -ndk flag set. แสดงว่า gomobile ของเราหา ndk ไม่เจอดังนั้นให้เราทำการรันคำสั่ง gomobile init ใหม่อีกครั้งพร้อมทั้งพ่วง path ของ ndk-bundle เข้าไปด้วย

$ gomobile init -ndk ~/Library/Android/sdk/ndk-bundle/

จากนั้นให้รันคำสั่ง

$ gomobile build -target=android golang.org/x/mobile/example/basic

ในที่สุดเราก็ได้ ไฟล์ APK มาแล้ว ต่อไปเราก็นำไฟล์ APK
ที่ได้ไปติดตั้งและทดสอบในมือถือของเราได้แล้ว

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay