Posts Tagged go

Fixing not working auto-completion for golang in Visual Studio Code

gophervsMy favorite code editor for GO  (Visual Studio Code with Go extenstion from lukehoban) on my Mac sometimes loses its intelligent behavior when suddenly code completion stops to work.

Usually just couple of commands in terminal recovers auto-completion back to life:

  1. Close Visual Studio Code
  2. Open terminal and stop gocode with gocode close
  3. Update gocode with the command `go get -u github.com/nsf/gocode`
  4. Start Visual Studio Code again and enjoy.

, , , , , ,

Leave a comment

Configure Ubuntu to work as GO web server

Картинки по запросуI’m often creating go apps or services using Digital Ocean droplets with Ubuntu, that’s why I decided to create a script to automate configuration process.

 

The script is doing following:

  1. Installs and configures firewall (ufw, allows only ports 80, 443 & 22)
  2. Installs git (required by go)
  3. Installs mysql and secures it with mysql_install_db
  4. Installs the latest version of GO (1.7.5), configures go environment (PATH, GOPATH)
  5. Installs go application from public git repository, yours or my demo (development for private is in progress) with ‘go get’
  6. Configures go app to work as a service (add config into /etc/init.d), starts the service
  7. Installs and configures nginx to work as proxy for go app

Script can be started directly from github:

bash <(curl -s https://raw.githubusercontent.com/bykovme/webgolangdo/master/preparegolangapp.sh)

Check the script and find more information here: https://github.com/bykovme/webgolangdo

, , , , , , , , , , , , , ,

Leave a comment

Localization for GOLANG

I created and published small package to be used for localization of go applications, you can find it here https://github.com/bykovme/gotrans

Installation

Install the package with the command

go get github.com/bykovme/gotrans

Prepare translation files

JSON files should use following format as in the example below:

{
    "hello_world":"Hello World",
    "find_more":"Find more information about the project on the website %s"
}

JSON file name should use standard language code or language-country code supported by browsers, if you are developing web app. At least one file with English locale should be in the localization folder with the name “en.json”.

Quick documentation

There are just 3 functions in the package

InitLocales(path string)

Use the relative or absolute path to set the folder where all the JSON files with translations are located. Make sure that all the files with translations have extension “.json”

Tr(lang string, key string) string

Get translation value by the language & key

DetectLanguage(acceptLanguage string) string

This function will be useful when you are creating web application, it detects the language from HTTP header Accept-Language, check the usage of the function in the example below

Example of using gotrans package

The same example is located within the package here

package main

import (
    "fmt"
    "net/http"

    "github.com/bykovme/gotrans"
)

func handler(w http.ResponseWriter, r *http.Request) {
    lang := gotrans.DetectLanguage(r.Header.Get("Accept-Language"))
    fmt.Fprintf(w, "<html><head><title> %s </title></head><body>", gotrans.Tr(lang, "hello_world"))
    fmt.Fprintf(w, "<h2> %s </h2>", gotrans.Tr(lang, "hello_world"))
    githubLink := "https://github.com/bykovme/gotrans"
    link := fmt.Sprintf(`<a href="%s">%s</a>`, githubLink, githubLink)
    fmt.Fprintf(w, gotrans.Tr(lang, "find_more"), link)
    fmt.Fprint(w, "</body></html>")
}

func main() {
    err := gotrans.InitLocales("langs")
    if err != nil {
        panic(err)
    }

    http.HandleFunc("/", handler)
    http.ListenAndServe(":3000", nil)
}

Behaviour

If the key is not found in the localization file, it will try to find the same key in English localization (“en.json”), if the key is not found there as well, the key will be returned instead of value.

, , , , , ,

Leave a comment

How to create simple webservice using GO (golang) on GAE

This tutorial will show how to create simple webservice application using GO on Google Application Engine (GAE), I’m going to cover the following topics:

  1. Creating project in GAE
  2. Creating and testing application locally
  3. Using martini package to simplify service development
  4. Deployment of the services to GAE

First of all, go to Google Developer Console and create new project:

https://console.developers.google.com/project

Screenshot 2015-09-09 09.22.54

Select project name and open advanced options, make sure that you are selecting the proper region you want to use, it is important as if we are going to add SQL Cloud later to our project and the region of the project & SQL Cloud should be the same to work together.

Screenshot 2015-09-09 09.29.28

Give some minutes to Google and you should see the picture as shown below with your project ready to be developed. Copy Project ID (in my case it is tutorial-golang, you will defenitely have different name) to you favourite text editor on your computer, we will need it later when we prepare our first deploy of the application

Screenshot 2015-09-09 09.35.42

Read the rest of this entry »

, , , , , , , , , ,

Leave a comment

Using GO + Sublime Text + GoSublime + GAE

Sometimes easy things are not so obvious as they really are. If you are using GO language together with Sublime Text and want to have in place such features as code completion, you obviously will start using GoSublime plugin. If you are using GO in GAE (Google Application Engine) for your developments than you need small tuning to make it work.

  1. First of all, make sure  GOROOT and GOPATH are set in your environment (or in Sublime Text settings) and pointing to the GAE version of GO
  2. Go to GOROOT/bin folder and make a symlink for “goapp” naming it “go”
  3. Go to GOROOT/pkg folder and make a symlink for “darwin_amd64_appengine” naming it “darwin_amd64”

Enjoy.

, , , , , , , , , , ,

1 Comment