Oracle by Example brandingConnect a Go Application to Oracle Database Cloud Service

section 0Before You Begin

This 15-minute tutorial shows you how to connect a Go application to Oracle Database Cloud Service and how to deploy your application to Oracle Application Container Cloud Service.

Background

Oracle Application Container Cloud Service lets you deploy Go, Java Platform, Standard Edition (Java SE), Node.js, PHP, Python, Ruby, and Enterprise Edition (Java EE) applications to the Oracle Cloud.

This tutorial provides you a sample application that creates a connection to Oracle Database Cloud Service. To create the connection it uses the environment variables DBAAS_USER_NAME, DBAAS_USER_PASSWORD, and DBAAS_DEFAULT_CONNECT_DESCRIPTOR that are provided by Oracle Application Container Cloud Service. The environment variables are created when you create the service binding. In this tutorial, you create the service binding by using a deployment.json file.

What Do You Need?


section 1Create a Go Project

In this section, you put together all the elements that are required to connect your Go application to Oracle Database Cloud Service.

  1. Create the go-oracle-db/oracle path directory in your local system.
  2. In the oracle directory, extract the content of the instantclient-basiclite-linux.x64-12.2.0.1.0.zip and instantclient-sdk-linux.x64-12.2.0.1.0.zip files.
  3. Copy the oracle-db.go and libaio1_0.3.110-1_amd64.deb files to the go-oracle-db directory.
  4. In the oracle-db\Oracle\instantclient_12_2 directory, create the oci8.pc file and add the following content:
    prefixdir=/u01/app/Oracle/instantclient_12_2
    libdir=${prefixdir}
    includedir=${prefixdir}/sdk/include
        
    Name: OCI
    Description: Oracle database driver
    Version: 12.2
    Libs: -L${libdir} -lclntsh
    Cflags: -I${includedir} 

section 2Prepare the Application for Cloud Deployment

  1. In the go-oracle-db directory, create the manifest.json, deployment.json, and start.sh files.
  2. Open the manifest.json file in a text editor and add the following content:
    {
        "runtime": {
            "majorVersion": "1.8.3"
        },
    	"command": "sh start.sh",
        "notes": "Go Application Sample"
    }
    
  3. Open the deployment.json file in a text editor and add the following content. Replace the placeholders with your information:
    {
      "services": [{
        "name": "Name of your Oracle Database Cloud Service instance",
        "type": "DBAAS",
        "username": "Your database user name",
        "password": "Your database password" 
      }]
    } 
  4. Open the start.sh file in a text editor and add the following content:
    # Extract LIBAOI libs from Debian package (into ./lib/x86_64-linux-gnu)
    dpkg-deb -R libaio1_0.3.110-1_amd64.deb ${APP_HOME}
    export PKG_CONFIG_PATH=${APP_HOME}/Oracle/instantclient_12_2
    
    # Add OCI and LIBAIO to shared library path
    export LD_LIBRARY_PATH=${APP_HOME}/Oracle/instantclient_12_2:${APP_HOME}/lib/x86_64-linux-gnu 
    
    # Finalize OCI installation by creating required softlink
    ln -s -f ${APP_HOME}/Oracle/instantclient_12_2/libclntsh.so.12.1 ${APP_HOME}/Oracle/instantclient_12_2/libclntsh.so
    ln -s -f ${APP_HOME}/Oracle/instantclient_12_2/libocci.so.12.1 ${APP_HOME}/Oracle/instantclient_12_2/libocci.so
    
    # Install Go dependencies
    go get github.com/mattn/go-oci8
    go get github.com/ant0ine/go-json-rest/rest
    
    # Launch the application
    go run oracle-db.go
  5. Create a zip file with the content of the go-oracle-db directory.

section 3Deploy the Application to Oracle Application Container Cloud Service

  1. Open the Oracle Application Container Cloud Service console.
  2. In the Applications list view, click Create Application and select Go.
  3. In the Application section, enter a name for your application and click Browse.
  4. On the File Upload dialog box, select the .zip file that you created in the previous section and click Open.
  5. Keep the default values in the Instances and Memory fields.
  6. Click More options, and then click Browse next to Deployment Configuration.
  7. On the File Upload dialog box, select the deployment.json file that you created in the previous section and click Open.
  8. Click Create.
  9. Wait until the application is created. The URL is enabled when the creation is completed.
  10. Click the URL of your application.
    GoSample application response
    Description of the illustration go-app-response.png

more informationWant to Learn More?