Skip to content
This repository was archived by the owner on Aug 9, 2025. It is now read-only.

Create project

tbeck edited this page Sep 17, 2023 · 2 revisions

Learn how to get started writing alt:V resources in Golang.


To develop resources using Golang, make sure to check out the following requirements and recommended tools.

Requirements

You need to have to following tools installed on your machine:

Initialize a new project

After installing Go, create a new empty folder and run go mod init to initialize a new project.
In this example we will call our project gofreeroom:

# Linux / macOS / Powershell (Windows)
mkdir gofreeroom 
cd gofreeroom 
go mod init gofreeroom

Add altv-go as dependency

To get the latest version of the go api run

go get -u github.com/timo972/altv-go@latest

Write serverside code

We are going to write a simple resource that spawns a player on connect. Create a file called main.go and paste the following content into it.

package main

import (
	"github.com/timo972/altv-go/altlog"
	"github.com/timo972/altv-go/entity"
	"github.com/timo972/altv-go/event"
)

func init() {
	event.On.PlayerConnect(onPlayerConnect)
	event.On.Start(onStart)
	event.On.Stop(onStop)
}

func main() {}

func onPlayerConnect(p entity.Player) {
	p.Spawn(0, 0, 76, 0)
}

func onStart() {
	altlog.Println("Hello from ~lb~go")
}

func onStop() {
	altlog.Println("bye")
}

Build the project

Building the resource is as easy as building a regular golang application. Simply define an output lib name and set buildmode to c-shared.

# Linux / macOS
go build -o gofreeroom.so -buildmode=c-shared
# Windows
go build -o gofreeroom.dll -buildmode=c-shared

The first build will likely run a little slower because the go compiler has to link to a c lib. All following builds will be at usual go speed.

Congratulations ๐ŸŽ‰! The gofreeroom.so (on linux) or gofreeroom.dll (on windows) library can now be run by the alt:V server.
Learn how to properly configure an alt:V resource to run the shared library

Clone this wiki locally