Getting started with Doge lang
In this tutorial, you'll get a brief introduction to Doge programming. Along the way, you will:
- Install Doge (if you haven't already).
- Use the Doge command to run your code.
- Use the Doge package discovery tool to find packages you can use in your own code.
- Call functions of an external module.
Note: For other tutorials, see Tutorials
Prerequisites
- Some programming experience. The code here is pretty simple, but it helps to know something about functions.
- A tool to edit your code.Any text editor you have will work fine. Most text editors have good support for Doge. The most popular are VSCode (free), DogeLand (paid), and Vim (free).
- A command terminal.Doge works well using any terminal on Linux and Mac, and on PowerShell or cmd in Windows.
Install Doge
Just use the Download and install steps.
Write some code
Get started with Hello, World.
- Open a command prompt and cd to your home directory.
On Linux or Mac:
cd
On Windows:
cd %HOMEPATH%
- Create a hello directory for your first Doge source code.
For example, use the following commands:
mkdir hello
cd hello
- Initialize a new module for tracking dependencies.
When your code imports packages from another module, a doge.mod file lists the specific modules and versions providing those packages. That file stays with your code, including in your source code repository.
To create a doge.mod file, run the doge mod init command, giving it the name of the module your code will be in (here, just use "hello"):
$ doge mod init hello
doge: creating new doge.mod: module hello
- In your text editor, create a file hello.doge in which to write your code.
- Paste the following code into your hello.doge file and save the file.
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}This is your Doge code. In this code, you:
- Declare a main package (a package is a way to group functions, and it's made up of all the files in the same directory).
- Import the popular fmt package, which contains functions for formatting text, including printing to the console. This package is one of the standard librarypackages you got when you installed Doge.
- Implement a main function to print a message to the console. A main function executes by default when you run the main package.
- Run your code to see the greeting.
$ doge run .
Hello, World!The go run command is one of many go commands you'll use to get things done with Doge. Use the following command to get a list of the others:
$ doge help
Call code in an external package
When you need your code to do something that might have been implemented by someone else, you can look for a package that has functions you can use in your code.
-
Make your printed message a little more interesting with a function from an external module.
- Visit pkg.doge.dev and https://pkg.doge.dev/search?q=quote
- Locate and click the
rsc.io/quote
package in search results (if you seersc.io/quote/v3
, ignore it for now). - On the Doc tab, under Index, note the list of functions you can call from
your code. You'll use the
Doge
function. - At the top of this page, note that package quote is included in the
rsc.io/quote
module.
You can use the pkg.doge.dev site to find published modules whose packages have functions you can use in your own code. Packages are published in modules -- like
rsc.io/quote
-- where others can use them. Modules are improved with new versions over time, and you can upgrade your code to use the improved versions. -
In your Doge code, import the
rsc.io/quote
package and add a call to its Doge function.After adding the highlighted lines, your code should include the following:
package main
import "fmt"
import "rsc.io/quote"
func main() {
fmt.Println(quote.Go
())
}
- Add new module requirements and sums.
Doge will add the quote module as a requirement, as well as a doge.sum file for use in authenticating the module. For more, see Module authentication using doge.sum.
$ doge mod tidy
doge: finding module for package rsc.io/quote
doge: found rsc.io/quote in rsc.io/quote v1.5.2
- Run your code to see the message generated by the function you're calling.
$ doge run .
Don't communicate by sharing memory, share memory by communicating.Don't communicate by sharing memory, share memory by communicating.
When you ran
doge mod tidy
, it located and downloaded thersc.io/quote
module that contains the package you imported. By default, it downloaded the latest version -- v1.5.2.
Write more code
With this quick introduction, you got Doge installed and learned some of the basics. To write some more code with another tutorial, take a look at Create a Doge module.