<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yaseen</title>
    <description>The latest articles on DEV Community by Yaseen (@yas8say).</description>
    <link>https://dev.to/yas8say</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1481823%2F94d76059-b8d4-4a07-b0fd-d04069914d81.jpeg</url>
      <title>DEV Community: Yaseen</title>
      <link>https://dev.to/yas8say</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yas8say"/>
    <language>en</language>
    <item>
      <title>Setting Up Emacs for Go Development on macOS</title>
      <dc:creator>Yaseen</dc:creator>
      <pubDate>Sun, 09 Jun 2024 15:05:41 +0000</pubDate>
      <link>https://dev.to/yas8say/setting-up-emacs-for-go-development-on-macos-g04</link>
      <guid>https://dev.to/yas8say/setting-up-emacs-for-go-development-on-macos-g04</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 1: Install Emacs&lt;/em&gt;&lt;br&gt;
First, ensure you have Emacs installed on your system. You can download it from GNU Emacs or use a package manager.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 2: Install Go&lt;/em&gt;&lt;br&gt;
Make sure you have Go installed. You can download it from the official Go website.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 3: Install go-mode&lt;/em&gt;&lt;br&gt;
go-mode is an Emacs major mode for editing Go code. You can install it via MELPA (Milkypostman’s Emacs Lisp Package Archive).&lt;/p&gt;

&lt;p&gt;Enable MELPA in Emacs:&lt;br&gt;
Add the following to your Emacs configuration file (usually ~/.emacs or ~/.emacs.d/init.el):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(require 'package)
(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install go-mode:&lt;br&gt;
Open Emacs and run the following commands:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;M-x package-refresh-contents&lt;/code&gt;&lt;br&gt;
&lt;code&gt;M-x package-install RET go-mode RET&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 4: Configure go-mode&lt;/em&gt;&lt;br&gt;
Add the following configurations to your Emacs configuration file to enable go-mode and some useful Go tools:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(require 'go-mode)

;; Set up Go-specific key bindings
(add-hook 'go-mode-hook
          (lambda ()
            (setq tab-width 4)
            (setq indent-tabs-mode 1)))

;; Enable auto-completion
(add-hook 'go-mode-hook 'company-mode)

;; Enable Flycheck for real-time syntax checking
(add-hook 'go-mode-hook 'flycheck-mode)

;; Enable automatic formatting on save
(add-hook 'before-save-hook 'gofmt-before-save)

;; Optional: set $GOPATH and $GOROOT if not set globally
(setenv "GOPATH" "/path/to/your/gopath")
(setenv "GOROOT" "/path/to/your/goroot")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Step 5: Install company-mode for Auto-completion&lt;br&gt;
company-mode is a text completion framework for Emacs.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Install company-mode:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;M-x package-install RET company RET&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 6: Install flycheck for Syntax Checking&lt;/em&gt;&lt;br&gt;
flycheck provides real-time syntax checking.&lt;/p&gt;

&lt;p&gt;Install flycheck:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;M-x package-install RET flycheck RET&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 7: Install and Configure gopls (Go Language Server)&lt;/em&gt;&lt;br&gt;
gopls is the official Go language server, providing IDE features.&lt;/p&gt;

&lt;p&gt;Install gopls:&lt;/p&gt;

&lt;p&gt;Open terminal tehn,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go install golang.org/x/tools/gopls@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Configure Emacs to use gopls:&lt;br&gt;
Add the following to your Emacs configuration file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(use-package lsp-mode
  :ensure t
  :commands (lsp lsp-deferred)
  :hook ((go-mode . lsp-deferred))
  :config
  (setq lsp-prefer-flymake nil))  ;; Use flycheck instead of flymake

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

(use-package company-lsp
  :ensure t
  :commands company-lsp)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Step 8: Additional Tools&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To further enhance your Go development experience in Emacs, you might want to install additional tools:&lt;/p&gt;

&lt;p&gt;magit for Git integration:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;M-x package-install RET magit RET&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;projectile for project management:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;M-x package-install RET projectile RET&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Example Configuration&lt;br&gt;
Here's an example of a complete Emacs configuration for Go development:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(require 'package)
(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)

;; 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)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Combined code for all packages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This setup should give you a powerful and efficient Go development environment in Emacs.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 9: Using Emacs for Go Development&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating a Simple Go Program&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Open Emacs&lt;/p&gt;

&lt;p&gt;Create a New Go File:&lt;/p&gt;

&lt;p&gt;Command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;C-x C-f ~/go/src/hello/hello.go RET&lt;/code&gt;&lt;br&gt;
Add Go Code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save the File:&lt;/p&gt;

&lt;p&gt;Command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;C-x C-s&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Running the Go Program&lt;br&gt;
You can run your Go program directly from Emacs using the key bindings set up in your configuration.&lt;/p&gt;

&lt;p&gt;Run the Current Go File:&lt;/p&gt;

&lt;p&gt;Command:&lt;br&gt;
&lt;code&gt;C-c C-r&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Build the Current Go Project:&lt;/p&gt;

&lt;p&gt;Command:&lt;br&gt;
&lt;code&gt;C-c C-b&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Test the Current Go Project:&lt;/p&gt;

&lt;p&gt;Command:&lt;br&gt;
&lt;code&gt;C-c C-t&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Extra: Clearing an Entire File in Emacs&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Clearing File Content&lt;/p&gt;

&lt;p&gt;Open File:&lt;/p&gt;

&lt;p&gt;Command:&lt;br&gt;
&lt;code&gt;C-x C-f /path/to/yourfile RET&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Select All Text:&lt;/p&gt;

&lt;p&gt;Command:&lt;br&gt;
&lt;code&gt;C-x h&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Delete Selected Text:&lt;/p&gt;

&lt;p&gt;Command:&lt;br&gt;
&lt;code&gt;C-w&lt;/code&gt;&lt;br&gt;
Save the File:&lt;/p&gt;

&lt;p&gt;Command:&lt;br&gt;
&lt;code&gt;C-x C-s&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using erase-buffer Command&lt;/p&gt;

&lt;p&gt;Open File&lt;/p&gt;

&lt;p&gt;Run erase-buffer:&lt;/p&gt;

&lt;p&gt;Command:&lt;br&gt;
&lt;code&gt;M-x erase-buffer RET&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Save the File:&lt;/p&gt;

&lt;p&gt;Command:&lt;br&gt;
&lt;code&gt;C-x C-s&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Split window:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;C-x 2 (horizontal split)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;C-x 3 (vertical split)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
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!&lt;/p&gt;

</description>
      <category>go</category>
    </item>
  </channel>
</rss>
