Golang : Display advertisement images or strings on random order
This is a Golang add on for my previous tutorial on how to display random advertisements with PHP by shuffling technique. If you are looking to display different content, images or strings each time a page load or refresh with Golang, use this example :
package main
import (
"fmt"
"math/rand"
"time"
)
func shuffle(src []string) []string {
final := make([]string, len(src))
rand.Seed(time.Now().UTC().UnixNano())
perm := rand.Perm(len(src))
for i, v := range perm {
final[v] = src[i]
}
return final
}
func main() {
ads := []string{
"<a href=\"http://www.edu.joshuatly.com\"><img src=\"https://www.hometuitionjob.com/public/images/ads/edujoshuatly250x250.gif\"></a>",
"<a href=\"http://www.guru-app.com\"><img src=\"https://www.hometuitionjob.com/public/images/ads/guru-app250x250.jpg\"></a>",
}
toDisplay := shuffle(ads)
// we only want to display 1st item, therefore use toDisplay[0]
fmt.Printf("Advertisement HTML code : \n %v\n", toDisplay[0])
}
Sample output :
Sweets-Mac-Pro:~ sweetlogic$ go run shuffleads.go
Advertisement HTML code :
<a href="http://www.guru-app.com">
<img src="https://www.hometuitionjob.com/public/images/ads/guru-app250x250.jpg">
</a>
Sweets-Mac-Pro:~ sweetlogic$ go run shuffleads.go
Advertisement HTML code :
<a href="http://www.edu.joshuatly.com">
<img src="https://www.hometuitionjob.com/public/images/ads/edujoshuatly250x250.gif">
</a>
Sweets-Mac-Pro:~ sweetlogic$ go run shuffleads.go
Advertisement HTML code :
<a href="http://www.edu.joshuatly.com">
<img src="https://www.hometuitionjob.com/public/images/ads/edujoshuatly250x250.gif">
</a>
Sweets-Mac-Pro:~ sweetlogic$ go run shuffleads.go
Advertisement HTML code :
<a href="http://www.edu.joshuatly.com">
<img src="https://www.hometuitionjob.com/public/images/ads/edujoshuatly250x250.gif">
</a>
Sweets-Mac-Pro:~ sweetlogic$ go run shuffleads.go
Advertisement HTML code :
<a href="http://www.guru-app.com">
<img src="https://www.hometuitionjob.com/public/images/ads/guru-app250x250.jpg">
</a>
See also : PHP : Shuffle to display different content or advertisement
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+13.3k Golang : Strings comparison
+15.4k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+15k Golang : How to get Unix file descriptor for console and file
+12.6k Golang : Convert int(year) to time.Time type
+20.2k Nginx + FastCGI + Go Setup.
+20.5k Golang : Convert PNG transparent background image to JPG or JPEG image
+16.2k Golang : Send email and SMTP configuration example
+5.2k Golang : Reclaim memory occupied by make() example
+10.6k Golang : Removes punctuation or defined delimiter from the user's input
+11.6k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+25.8k Mac/Linux and Golang : Fix bind: address already in use error
+11.2k Golang : Concatenate (combine) buffer data example