Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ASCII Art Generator (Go)

Introduction

This project is a simple Go program that converts normal text into ASCII art. ASCII art means drawing letters using symbols like o, |, -, /, etc.

Instead of printing plain text, the program prints a larger artistic version of the text using patterns stored in a file called standard.txt.

Example:

Input:

go run . "Hi"

Output (example):

Each character is built from multiple lines of symbols.


Project Files

Your project folder contains the following files:

ascii-art/
│
├── main.go        → Runs the program and handles user input
├── ascii.go       → Contains the AsciiArt function
├── standard.txt   → Contains the ASCII art patterns for characters
└── README.md      → Project documentation

How the Program Works

The program follows these steps:

  1. The user enters a word or sentence in the terminal.
  2. The program reads a file called standard.txt.
  3. This file contains ASCII drawings for every character.
  4. The program finds the drawing for each letter in the input.
  5. It combines the drawings and prints them in the terminal.

The ASCII Font File

The file standard.txt contains drawings for characters.

Each character is made of 8 lines of ASCII art, followed by one empty line.

So each character block uses 9 lines.

Example structure:

line 1 of character
line 2 of character
line 3 of character
line 4 of character
line 5 of character
line 6 of character
line 7 of character
line 8 of character

Characters are stored in ASCII order, starting from ASCII character 32 (space).


Code Explanation

Imports

The program uses these Go packages:

os
strings
log
fmt

These packages help the program:

  • read files
  • work with strings
  • print messages
  • handle errors

The AsciiArt Function

func AsciiArt(s string) string

This function takes a normal string and converts it into ASCII art.

Step 1 — Read the font file

content, err := os.ReadFile("standard.txt")

This loads the ASCII art patterns from the file.

If the file cannot be read, the program stops and prints an error.


Step 2 — Split the file into lines

arts := strings.Split(string(content), "\n")

The file content is split into individual lines and stored in a slice.


Step 3 — Handle newline characters

s = strings.ReplaceAll(s, "\\n", "\n")

This allows users to type \n to create a new line.

Example:

Input:

Hello\nWorld

Output:

Hello
World

Step 4 — Split input into lines

words := strings.Split(s, "\n")

If the user entered multiple lines, each line will be processed separately.


Step 5 — Prepare the output

result := strings.Builder{}

strings.Builder helps efficiently build the final ASCII art string.


Step 6 — Process each line

for _, word := range words

Each word or line is converted into ASCII art.


Step 7 — Handle empty lines

If a line is empty, the program simply prints a blank line.


Step 8 — Print ASCII rows

Each character drawing has 8 rows, so the program prints them one row at a time.

for row := 1; row <= 8; row++

Step 9 — Process each character

for _, ch := range word

The program looks at each character in the word.

Example:

Hello

Characters processed:

H
e
l
l
o

Step 10 — Find the ASCII art for the character

ascii := int(ch)
index := ascii - 32
start := index * 9

Explanation:

  • Every character has an ASCII number.
  • The ASCII art file starts at character 32 (space).
  • Each character uses 9 lines in the file.

This calculation tells the program where the drawing for that character starts.


Step 11 — Add the correct row to the result

result.WriteString(arts[start+row])

This adds the correct ASCII art line for the character.


Step 12 — Move to the next line

result.WriteByte('\n')

After printing one row for all characters, the program moves to the next line.


Step 13 — Return the result

return result.String()

The complete ASCII art is returned as a string.


The main Function

The main function is the entry point of the program.

It handles the input from the terminal.

Example:

go run . "Hello"

Step 1 — Check if an argument was provided

if len(os.Args) != 2

The program expects exactly one argument.

If none is provided, it prints:

No arguments provided!

Step 2 — Handle empty input

If the user enters an empty string, the program prints nothing.


Step 3 — Handle newline input

If the user enters \n, the program prints a blank line.


Step 4 — Generate ASCII art

fmt.Print(AsciiArt(args))

The input text is converted and displayed.


Running the Program

Go to the project folder and run:

go run . "Hello"

Example:

go run . "Go"

The terminal will display the ASCII art version of the text.


Requirements

You need:

  • Go installed on your system
  • The standard.txt file in the same folder as the program

Check your Go version:

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages