golang code splitting / local packages

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] okay we're gonna do some code splitting here with go okay let's clear this out all right so all right let's uh call us here op the video okay cd op video okay so open my favorite editors video studio code visual studio code all right damn phone all right all right first we're gonna what we're gonna do is we're gonna call because it should be more straightforward and go on how to call outside packages but it's not so i'm just going to um you know i'm going to show you guys today right all right so let's put in here we're going to start our main.go all right and so package main okay we're going to import format fmt like always and then we're gonna put uh we're gonna put our funk funk main all right perfect okay and we're gonna call we're gonna make another file and we'll call this uh well let's say let go okay all right perfect okay so and say well you know we won't import font and there we'll take it from here all right [Music] fmt and in order for this to work has to be in the same package so package package main so these both are in the same package main all right so we'll just name this um we'll just say say hello [Music] [Music] and it's kind of like this is a convention when it's going to be exported you want to put everything you want to put started you know starting with a capital letter right of the function name starting with a capital letter so in here we're just going to print line and we'll just say just say hello in all caps right now we should be able to do something like import say or something like that right but that's not going to work it's not going to work this is uh i thought this was kind of quirky about gold or whatever right okay so as long as it's in the same package package main and we got say in the same package we just call it regular like this right okay but what we have to do let me open up a terminal here okay what we have to do is we do go run we put the main dot go and then we have to put say let it go and then it'll print out hello if we don't put that say that go on the end it won't be able to find it so you just basically have to call it like that right and let's see we'll have if i do a gold build it should put out an executable and that'll be that you see and it'll put out that main executable we can do that dot slash main i'm one who want to and it shows just like that okay all right next we'll uh we'll include a package that's in another folder a local package okay now we're going to create another folder here and we're going to put this say dot go inside of that folder right so new folder and we'll call this t package okay for test package right and we'll put the say that go inside here right come on say that go go in there okay move okay goes in there let me let me just delete this and get this get this out the way all right get this um all right so we see in c and t package we should uh we have that say hello there right all right so in order for us to include this local package right we have to go mod init and name this package name this package you know whatever okay uh let's name it oop uh we'll call it video because that's the name of the folder you can name it anything else but just the name of the folders oop video so that's what i'm naming that okay perfect all right so now what we can do is we can import we have to start what oop video so that's t package okay yeah oop video that t package uh uh uh we got to bring that back we'll call that tp okay so we'll put tp dot what is that and say that to go and we got to change this to uh i forgot to say this we gotta change this to two package okay and it's say hello okay say hello okay it should work right here now [Music] you know what i think we have to add say i'll show you how to okay [Music] so we'll go go run package that say is not in root all right oop video two package let me see maybe it works like that all right technical difficulty okay what happened here was um it's correct all right but what we what i had here in this gold mod i had to spell the wrong so just make sure you have it spelled right and that's it so you'll be able to include say from here because it's in the main package you know the convention really is to uh just name it the same thing so name this q package.go okay that should still compile all right and then if we want to add another file in here now we can call this uh uh new file we'll call this say let go okay and we'll just give this something that returns the string so frank uh first let's say package package okay and then funk uh um let's put say name let's try to make this simple say name and it'll return a string okay so then we'll return let's say bob dole i don't know i just like that right all right so now we can uh when we go in this main package here we can also but you know what we'll just do it this way because it's the [Music] this is kind of the convention right here of doing it if you if you're importing multiple packages right so now we'll put s equal to oop video slash t package so let's say okay i don't know why this gives me an error but bring this down so we can see a little more of the screen so put s dot say name okay all right let's see what happens say is not in rule say go i think i have to put that under another uh let me try that let me put that under another folder called say and then we'll put it in here like that [Music] then let's try to run this again okay we still we still haven't we haven't we just i just tested it right there all right so we gotta in order to print it out we gotta put the bringing that um fmt package then print line because say name is only gonna say name is only gonna return a string bob though okay so there we go and bam we got bob though okay so that's how you split code and add local packages you know you can also go modern in here and then you know give it a github and then you'll be able to get it from github um you know maybe i'll do that in like the next video or like a video in the future but i just wanted to get out of the way getting local packages
Info
Channel: coderlyfe
Views: 209
Rating: undefined out of 5
Keywords: golang code splitting, include local packages golang, golang include code from another file, golang packages
Id: XZ7rpvnUTRc
Channel Id: undefined
Length: 10min 54sec (654 seconds)
Published: Sun Oct 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.