Setting Up Emacs for Go Development on macOS

RMAG news

Introduction

Emacs is a highly customizable text editor with powerful features for programming. In this guide, we’ll walk through installing Emacs on macOS, setting it up for Go development, and using it effectively.

Step 1: Installing Emacs on macOS

There are several ways to install Emacs on macOS. The easiest methods are using Homebrew or downloading a pre-built binary.

Using Homebrew
Open Terminal:
Command:
brew install –cask emacs
Downloading a Pre-Built Binary
Visit Emacs for macOS Website:

URL: https://emacsformacosx.com/
Download Latest Version

Drag to Applications Folder

Step 2: Installing Go
To develop Go applications, you need to have Go installed on your system.

Download the Go Installer:

URL: Go Downloads
Install Go

Verify Installation:

Command:

go version

Step 3: Setting Up Emacs for Go Development
Open Your Emacs Configuration File
Emacs uses a configuration file to customize its behavior. This file is usually located at ~/.emacs.d/init.el or ~/.emacs.

Open Emacs

Open the Configuration File:

Command:
elisp
Copy code
C-x C-f ~/.emacs.d/init.el RET
Add Configuration for Go Development
Copy and paste the following configuration into your init.el file:

elisp
Copy code
;; Initialize package sources
(require ‘package)
(add-to-list ‘package-archives
‘(“melpa” . “https://melpa.org/packages/”) t)
(package-initialize)

;; Ensure use-package is installed
(unless (package-installed-p ‘use-package)
(package-refresh-contents)
(package-install ‘use-package))

;; Install and configure go-mode
(use-package go-mode
:ensure t
:hook ((go-mode . lsp-deferred)
(before-save . gofmt-before-save))
:config
(setq tab-width 4)
(setq indent-tabs-mode 1))

;; Enable company-mode for auto-completion
(use-package company
:ensure t
:hook (go-mode . company-mode))

;; Enable flycheck for real-time syntax checking
(use-package flycheck
:ensure t
:hook (go-mode . flycheck-mode))

;; Configure lsp-mode and lsp-ui for Go
(use-package lsp-mode
:ensure t
:commands (lsp lsp-deferred)
:config
(setq lsp-prefer-flymake nil))

(use-package lsp-ui
:ensure t
:commands lsp-ui-mode)

(use-package company-lsp
:ensure t
:commands company-lsp)

;; Optional: projectile for project management
(use-package projectile
:ensure t
:config
(projectile-mode +1))

;; Optional: magit for git integration
(use-package magit
:ensure t)

;; Function to run the current Go file
(defun my-go-run ()
“Run the current Go file.”
(interactive)
(let ((compile-command (concat “go run ” buffer-file-name)))
(compile compile-command)))

;; Function to build the current Go project
(defun my-go-build ()
“Build the current Go project.”
(interactive)
(compile “go build”))

;; Function to test the current Go project
(defun my-go-test ()
“Test the current Go project.”
(interactive)
(compile “go test ./…”))

;; Add key bindings for Go commands
(add-hook ‘go-mode-hook
(lambda ()
(local-set-key (kbd “C-c C-r”) ‘my-go-run)
(local-set-key (kbd “C-c C-b”) ‘my-go-build)
(local-set-key (kbd “C-c C-t”) ‘my-go-test)))

;; End of configuration
Save and Reload the Configuration
Save the Configuration File:

Command:
elisp
Copy code
C-x C-s
Reload the Configuration:

Command:
elisp
Copy code
M-x eval-buffer RET
Step 4: Using Emacs for Go Development
Creating a Simple Go Program
Open Emacs

Create a New Go File:

Command:
elisp
Copy code
C-x C-f ~/go/src/hello/hello.go RET
Add Go Code:

go
Copy code
package main

import “fmt”

func main() {
fmt.Println(“Hello, World!”)
}
Save the File:

Command:
elisp
Copy code
C-x C-s
Running the Go Program
You can run your Go program directly from Emacs using the key bindings set up in your configuration.

Run the Current Go File:

Command:
elisp
Copy code
C-c C-r
Build the Current Go Project:

Command:
elisp
Copy code
C-c C-b
Test the Current Go Project:

Command:
elisp
Copy code
C-c C-t
Step 5: Clearing an Entire File in Emacs
Clearing File Content

Open File:

Command:
elisp
Copy code
C-x C-f /path/to/yourfile RET
Select All Text:

Command:
elisp
Copy code
C-x h
Delete Selected Text:

Command:
elisp
Copy code
C-w
Save the File:

Command:
elisp
Copy code
C-x C-s
Using erase-buffer Command

Open File

Run erase-buffer:

Command:
elisp
Copy code
M-x erase-buffer RET
Save the File:

Command:
elisp
Copy code
C-x C-s
Conclusion
By following this guide, you have set up Emacs on macOS for Go development, including installing necessary packages, configuring Emacs, and using it to write, run, build, and test Go programs. Happy coding!