Create Go apps using SQL Server on macOS
In this section, you will get SQL Server 2017 running on Docker on your Mac and then you will install the necessary dependencies to run GoLang.
Step 1.1 Install SQL Server
- In order to run SQL Server on your Mac, we are going to use the SQL Server on Linux Docker Image. For this, you need to install Docker for Mac.
- Configure at least 4GB of memory for your Docker environment, also consider adding multiple cores if you want to evaluate performance. You can do this in the Preferences - Advanced option on the menu bar.
- Next, start a new Terminal prompt and use the following commands to download and start the SQL Server on Linux Docker image. Make sure to use a strong password with special characters.
sudo docker pull microsoft/mssql-server-linux:2017-latest docker run -e 'HOMEBREW_NO_ENV_FILTERING=1' -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=yourStrong(!)Password' -p 1433:1433 -d microsoft/mssql-server-linux
You now have SQL Server running locally in Docker! Check out the next section to continue installing prerequisites.
Step 1.2 Install Homebrew and GoLang
If you already have GoLang installed on your machine, skip this step. Use the following commands to install Homebrew. Make sure to restart your terminal session once you’re done.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Restart the terminal session.
Update Homebrew and install GoLang.
brew update brew install go
Set up the GOPATH, GOROOT and GOBIN environment variables and add these to the PATH with the following commands:
echo "export GOPATH=$HOME/golang" >> ~/.bash_profile echo "export GOROOT=/usr/local/opt/go/libexec" >> ~/.bash_profile echo "export GOBIN=$GOPATH/bin" >> ~/.bash_profile echo "export PATH=$PATH:$GOPATH" >> ~/.bash_profile echo "export PATH=$PATH:$GOROOT/bin" >> ~/.bash_profile
Step 1.3 Install the ODBC Driver and SQL Command Line Utility for SQL Server
SQLCMD is a command line utility that enables you to connect to SQL Server and run queries.
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release brew update HOMEBREW_NO_ENV_FILTERING=1 ACCEPT_EULA=Y brew install msodbcsql17 mssql-tools
After installing SQLCMD, you can connect to SQL Server using the following command:
sqlcmd -S 127.0.0.1 -U sa -P your_password 1> # You're connected! Type your T-SQL statements here. Use the keyword 'GO' to execute each batch of statements.
This how to run a basic inline query. The results will be printed to the STDOUT.
sqlcmd -S 127.0.0.1 -U sa -P your_password -Q "SELECT @@VERSION"
-------------------------------------------------------- Microsoft SQL Server vNext (CTP2.0) - 14.0.500.272 (X64) Apr 13 2017 11:44:40 Copyright (c) Microsoft Corporation on Linux (Ubuntu 16.04) 1 rows(s) returned Executed in 1 ns
You have successfully installed SQL Server Command Line Utilities on your macOS machine!
You have successfully installed and setup GoLang and mssql-tools on your Mac. You now have everything you need to start writing your Go apps with SQL Server!